A Brief Talk on Exception Classification and Exception Handling Mechanism

Article directory


foreword

When we write code, we often encounter exceptions. This article briefly discusses the types and handling mechanisms of exceptions


1. What are the types of exceptions?

Top level exception: Throwable

Subclass exception: Error, such as OutOfMemoryError (OOM, memory overflow); Exception (this is our common)

The common subclasses of Exception are IOException, RuntimeException

Three common exceptions of RuntimeException:

NullPointerException - null pointer exception

ClassCastException - type conversion exception

IndexOutOfBoundsException - subscript out of bounds exception

The following figure is easy to understand, the relationship between each exception (only some of the commonly used exceptions are listed)


2. Exception handling mechanism (whoever uses it)

1. The current method declaration throws an exception

The code is as follows (example):

public class Demo01 {

    public static void main(String[] args) throws FileNotFoundException {

        FileInputStream fis = new FileInputStream("./test.java");
    }
}

The above is just for demonstration. It is not recommended to throw exceptions on the main method. Throwing exceptions on the main method is inconvenient to handle exceptions. 

2. Use the try...catch code block to wrap the adjustment code of this method

The code is as follows (example):

public class Demo02 {

    public static void main(String[] args){

        try {
            FileInputStream fis = new FileInputStream("./test.java");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Use try...catch as above to handle exceptions: alt+enter to select try/catch or select the code ctrl+alt+t to select try/catch

In addition, try...catch is sometimes used together with finally, that is, try...catch...finally, which will be demonstrated separately in the future


Summarize

1. If the called method throws a "non-RuntimeException", it must be handled, following a principle: Whoever calls it handles it

2. If an exception occurs, it must be handled. If it is not handled, there will be an error in the compilation, and the program cannot run

3. In the method we wrote ourselves, if an exception occurs, the general approach is to throw the exception, and whoever calls it will handle it, as shown below

class Demo03 {

    public void methodA() throws FileNotFoundException {
        //将异常抛出
        FileInputStream fis = new FileInputStream("./test.java");
    }

    public void methodB() throws FileNotFoundException {
        //调用了A方法,但是A方法将异常抛出,没有处理
        //所以调用A方法有异常存在
        methodA();
    }

    public void methodC(){
        try {
            //由于B方法依然将异常抛出,所以调用B方法仍存在异常
            //这里选择了处理异常
            methodB();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void methodD(){
        //由于C方法对异常进行了处理,所以调用C方法,不需要处理异常
        methodC();
    }
}

Guess you like

Origin blog.csdn.net/weixin_72125569/article/details/126683008