Exception: an error occurs during runtime.
Java exceptions are a mechanism provided by Java to handle errors in programs.
The so-called error refers to some abnormal events that occur during the running of the Program (for example, in addition to 0 overflow, the array subscript is out of bounds, and the file to be read does not exist)
Well-designed programs should provide methods to handle these errors when exceptions occur, so that the program will not be blocked or generate unforeseen results due to exceptions
If an exception occurs during Java program execution, an exception class object can be generated. The exception object encapsulates the information of the exception event and will be submitted to the Java runtime system, this process becomes throw exception
When the system receives an exception object during Java runtime, it will find the code that can handle this exception and hand it over the current exception object for processing. This process becomes a catch exception.
In Java, all exceptions have a common ancestor Throwable ). Throwable specifies the commonalities of any issues that can be transmitted through Java applications through the Exception Propagation mechanism in the code.
Throwable has two important sub-classes: Exception (Exception) and Error (Error). Both of them are important sub-classes for Java Exception Handling. Each of them contains a large number of sub-classes.
Error: An Error is generated and thrown by the Java Virtual Machine, including dynamic link failure and virtual machine Error. The program does not process the Error.
Exception: the parent class of the Exception class. Its sub-classes correspond to a variety of possible Exception events. Generally, You need to explicitly declare or capture
Runtime Exception: a special type of Exception, such as Division, array subscript out-of-bounds, which is frequently generated and troublesome to handle, explicit declaration or capture will significantly affect program readability and running efficiency. Therefore, the system automatically detects and delivers them to the default exception handling program (you do not have to handle it)
Exception capture and handling
Try statement
The Try {...} statement specifies a piece of code, which is a range of exceptions that can be captured and processed at a time.
During execution, the code segment may generate and throw one or more types of exception objects. The catch statements following the segment must handle these exceptions separately. If an exception is thrown, the statement after the exception statement is not executed in the try Block
If no exception occurs, all catch code segments are skipped and not executed.
Catch statement
The catch Block is the code that processes exceptions. Each try block can be used with one or more catch statements to process different types of exception objects that may occur.
The exception object declared in catch encapsulates the information about the exception event. You can use some methods of this object to obtain the information in the catch statement block.
For example:
The getMessage () method to obtain information about the exception event.
PrintStackTrace () method, used to trace the execution stack content when an exception event occurs
Finally statement
Finally statements provide a uniform exit for exception handling, so that the state of the program can be managed uniformly before the control process is transferred to other parts of the program.
No matter whether exceptions are thrown in the block specified by try, the Code specified by finally will be executed.
You can clear resources in finally statements, for example:
Close open files
Delete temporary files
There can be 0 or more Catch, and only one catch will be executed. Finally can only have 0 or 1! It must be written after catch.
After a try statement block, the capture statement of the basic class exception cannot be written on the top of the subclass exception capture statement.
The exception handling mechanism of Java allows the exception event to be searched forward along the called order until an exception handling program of this type is found.
Note:
During method rewriting, an exception of the same type as the exception thrown by the original method must be thrown or no exception is thrown.