Exceptions are some errors in the program, but not all errors are exceptions, and errors can sometimes be avoided.
For example, if your code is missing a semicolon, the result will be an error java.lang.Error; if you use System.out.println(11/0), then you are because If you divide by 0, a java.lang.ArithmeticException will be thrown.
There are many reasons for exceptions, which usually include the following categories:
The user entered illegal data.
The file to be opened does not exist.
The connection is interrupted during network communication, or the JVM memory overflows.
Some of these exceptions are caused by user errors, some are caused by program errors, and others are caused by physical errors. -
To understand how Java exception handling works, you need to master the following three types of exceptions:
Checked exceptions: The most representative checked exception is user error Or problems cause exceptions that the programmer could not foresee. For example, when trying to open a file that does not exist, an exception occurs. These exceptions cannot be simply ignored at compile time.
Run-time exceptions: Run-time exceptions are exceptions that may be avoided by programmers. In contrast to checked exceptions, runtime exceptions can be ignored at compile time.
Error: Error is not an exception, but a problem out of the programmer's control. Errors are often ignored in code. For example, when the stack overflows, an error occurs that cannot be checked during compilation.
An error that cannot be handled occurs when the program is running. Once it occurs, the JVM terminates execution.
Exception is an error that occurs when the program is compiled and run. Once it occurs, the JVM will tell the programmer to handle it. If handled properly, the program can still be executed. This is different from Error. Once an Error occurs, it cannot be handled and the program will terminate immediately.
is divided into two types:
#Runtime exception: occurs at runtime, RuntimeException class and subclasses. No processing is required during compilation, it occurs during the runtime phase. Common ones include NullPointerException\StringIndexOutOfBounds \ClassCastException \ArrayIndexOutOfBounds.
Compile-time exception (checked exception): an exception that must be handled when writing the code. If not handled, compilation will not pass. Compile-time exceptions have handling measures set up during the code compilation stage, so even if an exception occurs, it may not affect the normal operation of the program. Therefore, some systems regard compile-time exceptions as normal situations. Processing, for example, Spring submits normally when a compile-time exception occurs by default. Explicitly throwing exceptions in the program is equivalent to the exceptions that must be handled during the compilation stage, and is a compile-time exception. Common ones include ClassNotFoundException\FileNotFounException\ParseException\SQLException\IOException. The basis for dividing the two types of exceptions is the actual probability of occurrence. The JVM treats exceptions with a high probability of occurrence as checked exceptions, requiring programmers to write code
4.try...catch...finally...
#Put the statement block where an exception may occur after try. When an exception occurs, the try statement block stops executing, jumps to the catch statement block, and the catch statement block executes.
The finally statement block will be executed regardless of whether an exception occurs, and the code to close the resource is usually placed in it.
5. Custom exception
The above is the detailed content of Java exception handling graphic examples. For more information, please follow other related articles on the PHP Chinese website!