Java Exception Handling Mechanism
I. java exception Classification
Java exception:
(1) Error: a serious problem that cannot be solved by the Java Virtual Machine
(2) Exception: the NULL pointer is abnormal and the network connection to the file that does not exist is interrupted.
Both Error and Exception inherit the sub-java. lang. throwable
Error Type:
1. java. lang. StackOverflowError
Public class TestError {
Public static void main (String [] args ){
Main (args );
}
}
2. java. lang. OutOfMemoryError
Public static void main (String [] args ){
Byte [] B = new byte [10241024600];
}
Exception:
// Common runtime exceptions
// 1. array subscript exception java. lang. ArrayIndexOutOfBoundsException: 10
@ Test
Public void test (){
Int [] I = new int [10];
System. out. println (I [10]);
}
// 2. Arithmetic exception: arithmeticException
@ Test
Public void test2 (){
Int I = 10;
System. out. println (I/0 );
}
// 3. type conversion exception: java. lang. ClassCastException: java. util. Date cannot be cast
Java. lang. String
@ Test
Public void test3 (){
Object obj = new Date ();
String str = (String) obj;
}
// 4. NULL pointer exception java. lang. NullPointerException
@ Test
Public void test4 (){
Object s = new String ("abc ");
S = null;
System. out. println (s. toString ());
}
// 5. Run at compile time
@ Test
Public void test5 () throws Exception {
FileInputStream FCM = new FileInputStream (new File
("Hello.txt "));
Int B;
While (B = FCM. read ())! =-1)
{
System. out. println (char) B );
}
}
Ii. How to Handle Exception exceptions
Java provides an exception capture and throwing Model
Step 1:
"Throw": When we execute code, once an exception occurs, a corresponding exception class object will be generated at the Exception Code.
And throw the object (automatically or manually)
> Once an exception class object is thrown, the program terminates execution.
> The object of this exception class is thrown to the method caller.
2. "catch": capture the exception class Object thrown in the previous step. How to catch it is the exception handling method
Two methods are provided to handle abnormal objects.
> Method 1:
Try {
// Code that may contain exceptions
}
Catch (Exception ex ){
}
Catch (Exception ex2 ){
}
Finally {
}
Note:
1. variables declared in try, similar to local variables, cannot be called except for try statements.
2. The finally statement is optional.
3. The catch statement processes the exception object internally.
There are two available methods: getMessage () printStackTrace ();
4. There can be multiple catch statements. The exception class Object thrown in try matches the exception type in catch from top to bottom.
Then, execute the code in the catch statement. After the execution, multiple catch statements will pop out.
5. If the exception is handled, the subsequent code continues to be executed.
6. If multiple exception types in the catch object are in the "Parallel" relationship, the order is acceptable;
If it is an include relation, the subclass is above
7. The finally statement will be executed, regardless of whether the catch statement has an exception or return statement.
8. try catch can be nested with each other.
> Method 2:
In the method declaration, the throws statement is used to declare the list of thrown exceptions. The exception type declared by throws can be an exception in the method.
It can also be its parent class.
Format: public static void method2 () throws IOException
When an exception occurs inside the method, an exception object is thrown to the method caller.
An exception object can be thrown up layer by layer until it is in main. Of course, during the process of throwing up, try-catch can be used again.
Management
3. Exceptions during running can be handled explicitly
You must handle exceptions during compilation.
Conclusion: java Exception Handling adopts the catch and throw model.
1. catch: There are two ways to handle exceptions (try-catch-finally throws + exception type)
2. throw: if an exception occurs during execution, an object of the exception class is thrown (throw + exception is thrown automatically.
Item)
> The exception class can be either a ready-made exception class or a self-created exception class.
4.
How to customize exception classes:
// 1. Let the custom exception class inherit the existing exception class
Public class MyException extends RuntimeException {
// 2. Provide a serial number and several overloaded Constructors
Static final long serialVersionUID =-7034897190745766939L;
Public MyException (){
}
Public MyException (String msg ){
Super (msg );
}
}
The subclass overrides the parent class method. The exception type thrown by the subclass can only be the subclass or exception class of the method to be overwritten. That is, subclass
The range of thrown exceptions is smaller than that of the parent class.