Summary of java exception system

Reference article: https://snailclimb.top/JavaGuide/#/
In Java, all exceptions have a common ancestor Throwable class in the java.lang package . Throwable: There are two important subclasses: Exception and Error, both of which are important subclasses of Java exception handling, each of which contains a large number of subclasses:
Insert picture description here

Error:

It is an error that the program cannot handle, which means a serious problem in running the application. Most errors are not related to the actions performed by the code writer, but indicate problems with the JVM (Java Virtual Machine) when the code is running . For example, Java Virtual Machine Error (Virtual MachineError), when the JVM no longer has the memory resources required to continue the operation, OutOfMemoryError will occur. When these exceptions occur, the Java Virtual Machine (JVM) generally chooses to terminate the thread.

These errors indicate that the failure occurred in the virtual machine itself or when the virtual machine tried to execute the application, such as Java Virtual Machine Error (Virtual MachineError), class definition error (NoClassDefFoundError), etc. These errors are undetectable because they are outside the control and processing capabilities of the application, and the vast majority are conditions that are not allowed while the program is running. For a properly designed application, even if an error does occur, it should not attempt to deal with the abnormal conditions caused by it. In Java, errors are described by Error subclasses.

Exception:

It is an exception that the program itself can handle. The Exception class has an important subclass RuntimeException. RuntimeException is thrown by the Java virtual machine. NullPointerException (the variable to be accessed does not refer to any object, the exception is thrown), ArithmeticException (arithmetic exception, an integer is divided by 0, the exception is thrown) and ArrayIndexOutOfBoundsException (subscript out of bounds exception)
In fact, exceptions can be simply divided into runtime exceptions and non-runtime exceptions.
Non-runtime exceptions are exceptions other than RuntimeException , and all types belong to the Exception class and its subclasses. From the perspective of program syntax, it is an exception that must be handled. If it is not handled, the program cannot be compiled. Such as IOException, SQLException, etc. and user-defined exceptions, under normal circumstances do not custom check exceptions, in simple terms, you need to be told by the compiler that you need to try ... catch exceptions are basically non-runtime exceptions, such as io exceptions, File cannot be found abnormal, etc .;

The runtime exception is RuntimeException , which is generally an exception that may be logically generated by a bug. It is thrown by the virtual machine. The program can choose to capture or not handle it. The program should avoid such exceptions from a logical perspective. Occurs, such as a null pointer, an out-of-bounds index, a file not found exception, etc.

Catch exception

  • try block: Used to catch exceptions. It can be followed by zero or more catch blocks. If there is no catch block, it must be followed by a finally block.
  • catch block: Used to handle exceptions caught by try.
  • Finally block: Whether the exception is caught or handled, the statement in the finally block will be executed. When a return
    statement is encountered in a try block or a catch block , the finally statement block will be executed before the method returns.

In the following 4 special cases, the finally block will not be executed:

  1. An exception occurred on the first line of the finally block. Because in other lines, finally block will still be executed

  2. Used System.exit (int) in the previous code to exit the program. exit is a function with parameters; if the statement is after an abnormal statement, finally will be executed

  3. The thread where the program is located dies.

  4. Turn off the CPU.

Note: When both the try statement and the finally statement have a return statement, the content of the finally statement will be executed before the method returns, and the return value of the finally statement will overwrite the original return value. as follows:

   public static int f(int value) {
        try {
            return value * value;
        } finally {
            if (value == 2) {
                return 0;
            }
        }
    }

If you call f (2), the return value will be 0, because the return value of the finally statement overwrites the return value of the try statement block.

Custom exception

Code directly

package test;

public class MyException {
    public static void main(String[] args) {
        demo d = new demo ();
        int a = 10;
        int b = -1;
        try {
            int div = d.div (a,b);
            System.out.println (div);
        }catch (fuShuException e){
            System.out.println ("除数为"+b);
            System.out.println (e.getMessage ());
        }

    }
}
class fuShuException extends Exception{
    fuShuException(String msg){
        super(msg);
    }
}
class demo {
    public int div(int a, int b)throws fuShuException{
        if( b<= 0){
          throw  new   fuShuException("除数不能为负数");
        }
        return a/b;
    }
}

Custom exceptions directly inherit the Exception class, rewrite the constructor if necessary, and then use the keyword throws to specify exceptions where exceptions need to be thrown

  1. Custom exception:

    class Exception class name extends Exception
    {
    public Exception class name (String msg)
    {
    super (msg);
    }
    }

  2. Identify possible exceptions:

    throws exception class name

  3. Catch exception:

    try {}
    catch (Exception class name y) {}

  4. Method explanation

    getMessage () // Output abnormal information
    printStackTrace () // Print heap trace

Published 39 original articles · won praise 1 · views 4620

Guess you like

Origin blog.csdn.net/thetimelyrain/article/details/100006079