Exception Capture statement:
Try-catch (multiple catch block)-finally
Code format:
Try-catch and Try-catch-finally
try{
Some exceptions that will be thrown
}catch (Exception e) {
The code block that handles the exception
}finally{
The final code to execute
}
Statement Example:
1 Try {2 double d = 5/0; 3 }catch(Exception e) {4 System.out.println ("Arithmetic exception"); 5 }finally{6 System.out.println ("finally statement block"); 7 }
Try-catch Statement Example
Statement interpretation:
Try block : is responsible for catching exceptions, and once an exception is found in the try, the control of the program is handed over to the exception handler in the CATCH block.
Note: A try statement block may not exist independently and must be in the same memory as a catch or finally block
Catch block : How do I handle it? For example, warn: prompt, check configuration, network connection, record error, etc. After executing the catch block, the program jumps out of the catch block and proceeds with the subsequent code.
Note: The exception class that is handled by multiple catch blocks is treated as if the parent class is handled by the catch subclass, because the exception is handled "nearest" (from top to bottom).
finally block : The final code to be executed, to close and release resources, etc.
Exception handling:
Execution is aborted by the exception handler (throwing a reminder or logging, etc.), and the code outside the exception code is executed normally.
Try throws many types of exceptions, and multiple catch blocks capture multiple-clock errors.
Multiple exception handling code block order problem: Subclass again Parent class (wrong order will also alert error), finally statement block handles the code that will eventually execute
Summarize:
1, when dealing with abnormal operation, using logic to avoid the simultaneous auxiliary try-catch processing
2. After multiple catch blocks, you can add a catch (Exception) to handle exceptions that may be missed
3, for the indeterminate code, you can also add try-catch, handle the potential exception
4, try to deal with the exception, remember just simple call Printstacktrace () to print
5, how to deal with the exception, according to different business needs and the type of exception to decide
6, try to add a finally statement block to release the resources occupied
Java Exception capture