------- Android training, Java training, and hope to communicate with you! ----------
To improve the fault tolerance and robustness of the program, an exception mechanism is introduced. When an exception occurs in the program, an exception object notification program is automatically generated to separate the normal code from the error handling code, improve readability.
Keyword for exceptions in Java:
Try catch finally throw throws
Relationship between exceptions:
Throwable
| --- Error. JVM-related issues cannot be handled without catch and throws declarations.
| --- Exception
| --- Runtimeexception
| --- Checked exception
Runtimeexception: exception during running
A running exception indicates that the caller cannot handle or recover program errors.
Depending on the running status, the program will not be detected during compilation. It can be automatically thrown and captured by the system, and the program will be terminated directly.
Try, catch, and throws exception statements are not required,
Exceptions during running can also be captured.
If captured, program code errors (bugs) are concealed and cannot be noticed during running.
Common exceptions include division by zero, null pointer, and array out-of-bounds.
Checked exception: exception detected during compilation
The caller must display the capture processing or display the Declaration to throw an exception.
Catch:
Exception Capture: capture small exceptions first and then large exceptions.
Multiple exception capture in Java 7:
Catch (exception1 | exception2 e) {...} Use | to separate exception types. e is modified by final by default.
Exception Handling Methods:
1. Handle the exception through catch, fix the exception, and continue to execute the program;
2. After catch, a new exception is thrown and handed to the upper-layer caller;
3. Do not capture exceptions. Throws are directly thrown using the throws declaration.
The caught exception is assigned to the catch parameter. The methods for this exception object include:
Getmessage (): returns the description string of the exception.
Printstacktrace (): the error stack information is output to the standard output.
Printstacktrace (printstream PS): the error stack information is output to the specified output stream.
Getstacktrace (): returned stack information, array
Finally code block:
Used to close resources.
When a return or throw statement is encountered, the code in finally is executed before the method is returned;
If the catch code block contains the system. Exit (1); Statement, the finally code block will not be executed.
If you need to define the code that must be executed, you can use try... Finally .. Statement to put the code in the Finally block.
Java 7 automatically closes the try statement of the Resource
Try (define resource or initialize Resource ){....}
These resources must implement the closeable or autocloseable interfaces;
It contains the Finally block used to close the resource. You do not need to catch or finally block.
Throw statement
If it exists independently, it cannot be followed by the statement because it cannot be executed.
Custom exception: exception or runtimeexception must be inherited.
Throws throw an exception
The exception thrown by the subclass method should be the subclass or the same of the exception thrown by the parent class method. The subclass method cannot throw more exceptions than the parent class method.
For example, during polymorphism, early programs cannot handle new exceptions later.
------- Android training, Java training, and hope to communicate with you! ----------