Exception
Published:
Java basics – Exception
Exception
Abnormal conditions that occur during the execution of the program will eventually cause the JVM to stop abnormally
Exception system
The root class of the exception is java.lang.Throwable, which has two subclasses java.lang.Error and java.lang.Exception
- Error: Serious errors, errors that cannot be handled, can only be avoided in advance
- Exception: exception, which can be corrected by means of code to make the program run normally
Exception handling
throw keyword
You can use throw to throw the specified exception in the specified method
Use format: throw new xxxException("the reason for the exception");
note:
The throw keyword must be written inside the method
Throw object must be Exception or its subclass object
The throw keyword throws the specified exception object, you must handle the exception
What is thrown after the throw keyword is the RuntimeException object or its subclass object, we can leave it to the JVM to handle it by default (print the exception object, interrupt the program)
What is thrown after the throw keyword is a compilation exception, we must deal with this exception (throws or try catch)
Objects non-empty judgment
Objects are null pointer safe, there are static methods in the source code to determine whether it is a null pointer
public static <T> T requireNotNull(T obj){
if(obj==null)
throw new NullPointerException();
return obj;
}
throws keyword
The first way to handle exceptions is to be handled by others
effect:
- When an exception object is thrown inside the method, we must handle the exception object
- You can use the throws keyword to handle the exception object, throw the exception object declaration to the caller of the method, and finally hand it over to the JVM for processing
Usage format: used in method declaration
Modifier return value type method name (parameter list) throws exception object {method body}
Precautions:
throws must be written in the method declaration
The object declared by throws must be Exception or its subclass object
If multiple exception objects are thrown inside the method, then multiple exceptions must be declared after throws
If multiple exception objects thrown have a child-parent relationship, just declare the parent exception directly
Calling a method that declares to throw an exception, we must handle the declared exception
Or continue to use throws to throw exceptions, hand it over to the caller of the method, and finally hand it over to the JVM
Either try catch to handle the exception yourself
Catch exceptions
The second way of try…catch exception handling is to handle the exception yourself
format:
try{
Code that may produce an exception
}
catch (exception data type variable name) {
Exception handling logic
}
Precautions:
Multiple exception objects may be thrown in try, then multiple catches can be used to handle these exceptions
If an exception occurs in try, the exception handling logic in catch will be executed, and the code after try…catch will continue to execute
If there is no exception in the try, then the exception handling logic in the catch will not be executed. After executing the code in the try, continue to execute the code after the try…catch
Methods of handling exceptions in Throwable
String getMessage()
: returns a short description of this throwableString toString()
: Returns the detailed description string of this throwablevoid printStackTrace()
: JVM prints the exception object, this method is the default, the exception information printed is the most comprehensive
finally code block
format:
try{
Code that may produce an exception
}
catch (exception data type variable name) {
Exception handling logic
}
finally{
It will be executed regardless of whether there is an exception
}
Precautions:
- Finally cannot be used alone, it must be used together with try
- Finally is generally used for resource release
Exception notice
Multiple exception capture processing
Multiple exceptions are handled separately
Multiple exceptions are caught once and processed multiple times
If the exception variable defined in the catch has a child-parent relationship, the exception variable of the subclass must be written on it
Multiple exceptions are caught once and processed once
Custom exception class
format:
public class XXXException extends Exception | RuntimeException{
Add an empty parameter constructor
Add a construction method with exception information
}
note:
The custom exception class generally ends with Exception, indicating that the class is an exception class
The custom exception class must inherit Exception or RuntimeException