Basics of JAVA--Exception Packaging Technology

Applicable scene:

1. When writing a project, the more you go to the bottom, the more exceptions are thrown, and the parent class needs to handle these exceptions. It is too troublesome to catch every exception and also consider the problem of the parent-child relationship of the exception class , of course we can also directly capture their superclass IOException . We can create an exception class by ourselves, encapsulate the underlying exception, and throw the new exception class uniformly. When catching, we only need to catch the exception and output all error messages.
2. If a checked exception (that is, a compile-time exception) occurs in a method, but this method does not allow a checked exception to be thrown, then packaging techniques are also useful. We can catch this checked exception and wrap it into a runtime exception.
3. Change the type of exception thrown.

Simulate scenario 1, throw encapsulated exceptions from the bottom layer to the upper layer
, first customize an exception class, inherit the superclass IOException of compile-time exceptions, rewrite initCause and getCause just to print the next two sentences to prove that the package is indeed carried out, the actual No need to rewrite at all.

public class ServletException extends IOException {
    
    
    public ServletException() {
    
    
    }

    public ServletException(String message) {
    
    
        super(message);
    }

    @Override
    public synchronized Throwable initCause(Throwable cause) {
    
    
        System.out.println("重新初始化了异常");
        return super.initCause(cause);
    }

    @Override
    public synchronized Throwable getCause() {
    
    
        System.out.println("输出初始化异常");
        return super.getCause();
    }
}

Simulate the underlying throwing exception and use initCause to initialize the exception
ServletException encapsulates FileNotFoundException and throws the exception.

public static void test() throws ServletException{
    
    
        int arr[] = new int[]{
    
    1,3,4};
        if (arr[0] == 1) {
    
    
            FileNotFoundException e = new FileNotFoundException("e1异常");
            ServletException se = new ServletException("se异常");
            se.initCause(e);
            throw se;
        }
    }

The method caller, that is, the upper layer of this layer performs exception handling

public static void main(String[] args) {
    
    
        try {
    
    
            test();
        } catch (IOException se) {
    
    
            se.printStackTrace();
        }
    }

insert image description here
As can be seen from the above results, the exception is encapsulated, and the exception information is printed at the same time. The getCause knowledge judges the exception, and all methods call the getCause method to determine the cause of the throwable.

The same is true for scene 2 and scene 3. You can change the inheritance type of the custom exception to change the exception type and then encapsulate it. According to your choice, it is a runtime exception or a compile-time exception.

Thanks to the blogger of this blog:
Instructions on the usage of initcause in Java

Guess you like

Origin blog.csdn.net/qq_44660367/article/details/108938268