Abnormal
I. Unexpected results of behavior can be divided into two categories
Unexpected results that can be handled are called exceptions (Exception)
Unexpected results that cannot be handled are called errors (Error)
An exception is a special object of type Java.lang.Exception or subclass of a class.
two. Handling
1. Catching exceptions
Try--> executing code that could produce an exception
Catch--> Catching exceptions
Finally--> code always executes regardless of whether an exception occurs
2. Declaring exceptions
Throws--> declaring a method may throw various exceptions
3. Throwing Exceptions
Throw--> Manually throwing Exceptions
Three. Try-catch
Situation One:
Try (no exception) Catch Try-catch post code
| |
---------------------------
Situation Two:
Try (with exception) catch---->try-catch code
| |
--Produces an exception object--the exception type matches--
Situation Three:
Try (with exception) catch---->try-catch code
|
--Generate Exception Object--Exception type mismatch--code break
Four. Handling exceptions in Catch blocks
1. Add Custom processing information
SYSTEM.ERR.PTINTLN ("Custom Content")
2. Calling method Output exception information
E.printstacktrace ();
3. Common methods for Exception objects
String GetMessage ();
* Return exception information description string is part of the printstacktrace () output information
Five. Java exception mechanism
1. When the code runs to a possible exception, the exception object is thrown by the program or Java, and the exception object records the relevant information
2. After the exception object is thrown, it will look for the exception handling related code (catch), if the end of the exception is found
3. If not found, the exception object will continue to throw up, looking for exception handling code
4. If the exception is thrown to the exception handler code, then main throws the exception to the virtual machine.
5. Each layer of code thrown by the exception will be interrupted
Six. Common exception types
Exception type description
Exception the parent class of the exception hierarchy
ArithmeticException arithmetic error cases, such as dividing by 0
Arrayindexoutboundsexception array subscript out of bounds
NullPointerException attempting to access a null object member
ClassNotFoundException cannot load the required classes
The IllegalArgumentException method received an illegal parameter
ClassCastException object coercion type conversion error
NumberFormatException numeric format conversion exceptions, such as converting "ABC" to Digital
Seven. Finally
1. If an exception occurs, the code in the finally block will be executed
2. The only non-implementation situation: System.exit (1);
Eight. Try-catch-finally block with return
Try---> Generate exception Object---> Exception type match---> enter catch--->
Execute finally---> Execute return exit method
Nine. Try, catch, finally use rules
try{
Must appear and only occur once
}catch{
Can appear 0~n times, if there is no catch there must be a finally
}finally{
Can appear 0~1 times
}
10. Multiple Catch blocks
1. Order of the Catch statements: first Subclass stepfather Class
2. Match-by-order when an exception occurs
3. Execute only the first catch statement that matches the exception type
Eleven. Java Exception classification
1. Runtime exception: No detection at code compile time, exception occurs during code runtime
Both the RuntimeException and its subclasses belong to a runtime exception,
And ArithmeticException, Nullpointeexception, NumberFormatException.
2. Check for exception/detect exception: The code compiles when the display must be processed, in order to be compiled by the compiler,
such as: SQLException, classnofoundexception
12. Throw, Throws
1.throws declares various exceptions that a method may throw
2.throws lets you check for exceptions through compiler compilation, but does not really resolve exceptions
3.throw throws an exception manually and can define the information for the exception
4.throws can declare multiple exceptions, with multiple exceptions separated by commas
13. Custom Exceptions
1. Write an exception class, inherit exception
2. Manual implementation of Exception construction method
Java (v)