Detailed explanation of exception system in JAVA

Anomaly classification

General exceptions can be divided into the following categories:
1. User input error
Unexpected user input content
2. Device error
IO device error, hard disk damage, etc.
3. Physical limitations
Disk space is full
4. Code error
references a null pointer .

Java exception classification

Java exception system

When to declare checked exceptions

There are four scenarios for throwing the checked exception:
1. Calling a method that throws the checked exception
2. An error is thrown when the program is running, and an exception is thrown using the throw statement
3. An error occurs in the program
4. The Java virtual machine and the runtime library appear internal error of

How to throw an exception

After an exception is thrown, Java will use the new method to create an exception object on the heap. The current path of execution is terminated, popping the reference to the exception object from the current environment. At this point, the exception handling mechanism takes over the program and starts looking for an exception handler to continue program execution. The task of this exception handler is to recover the program from the error state so that the program can either run in a different way or continue to run. go down.
For an existing exception class, if an exception occurs, the following process will be triggered
: 1. Find a suitable exception class
2. Create an object of this class
3. Throw the object

catch exception

Use the try {} catch() {} structure to catch exceptions, the program will skip the rest of the code in the try statement block, and the program will execute the processor code in the catch clause.
The compiler strictly enforces the throws specifier, and if an exception is invoked, it must be handled, or passed on.

Guess you like

Origin blog.csdn.net/sinat_28199083/article/details/125163057