Java exception handling Exception

1.java.lang.Throwable interface

|---java.lang.Error: A serious problem that cannot be solved by the Java virtual machine. Such as: JVM system internal error, resource exhaustion and other serious situations. For example: StackoverflowError and OOM. Generally do not write targeted code for processing

|---java.lang.Exception: can handle exceptions

|----Compile-time exception (checked)

|----IOException

|----FileNotFoundException

|----ClassNotFoundException

|----Runtime exception (unchecked)

|----NullPointerException

|----ArrayIndexOutofBoundsException

|----ClassCastException

|----NumberFormatException

|----InputMismatchException

|----ArithmeticException

2. Abnormal handling: catch and throw model

Process 1: "Throwing": During the normal execution of the program, once an exception occurs, an object corresponding to the exception class will be generated at the exception code, and this object will be thrown. Once the object is thrown, the subsequent code is no longer executed.

Regarding the generation of exception objects:

Exception objects automatically generated by the system

Manually generate an exception object and throw (throw)

Process 2: "catch": It can be understood as an abnormal handling method:

1.try-catch-finally

2. throws

3. The first method of exception handling: try-catch-finally
try{
    可能出现异常的代码
}catch(异常类型1 变量1){
    处理异常的方式1
    变量1.printstackTrace()
}catch(异常类型2 变量2){
    处理异常的方式2
}catch(异常类型3 变量3){
    处理异常的方式3
    变量3.printstackTrace()
}
.....
finally{
//一定执行的代码
}

illustrate:

1.finally is optional.

2. Use try to wrap possible exception codes. During execution, once an exception occurs, an object corresponding to the exception class will be generated. According to the type of the pair, match it in the catch

3. Once the exception object in try matches a certain catch, it enters the catch to handle the exception. Once the processing is completed, jump out of the current try-catch structure (in the case of not writing finally), and continue to execute the subsequent code.

4. If the exception type in the catch has no child-parent relationship, it doesn't matter who declares above and who declares below. If the exception type in the catch satisfies the sub-parent class relationship, the sub-class must be declared above the parent class. Otherwise, report an error

5. Commonly used exception object handling methods: 1. String getMessage() 2. printstackTrace()

6. Variables declared in the try structure, after the try structure, can no longer be called

7. try-catch can be nested with each other

Experience 1: Use try-catch-finally to handle compile-time exceptions, so that the program will no longer report errors at compile time, but errors may still be reported at runtime. It is equivalent to using try-catch-finally to delay an exception that may occur at compile time until runtime.
Experience 2: In development, because runtime exceptions are more common, we usually don't write try-catch-finally for runtime exceptions. For compile-time exceptions, we say that exception handling must be considered.
4. The second method of exception handling: throws + exception type

1. "throws + exception type" is written at the declaration of the method. Indicates the type of exception that may be thrown when this method is executed. Once an exception occurs when the method body is executed, an object of the exception class will still be generated at the exception code. When this object meets the exception type after throws, it will be thrown. The code following the exception code will no longer be executed!

2. Experience:

  • try-catch-fina1ly: Really handle the exception

  • The method of throws just throws the exception to the caller of the method. does not really handle the exception

3. How to choose to use try-catch-finally or throws during development?

3.1 If the overridden method in the parent class does not have throws to handle exceptions, the method overridden by the subclass cannot use throws, which means that if there is an exception in the overridden method of the subclass, it must be handled using try-catch-finally .

3.2 In the executed method a, several other methods are called successively, and these methods are executed in a progressive relationship. We recommend that these methods use the throw method for processing. The executed method a can be considered to use the try-catch-finally method for processing.

4. Manually throw an exception: throw

public class ExceptionTest {
    public static void main(String[] args) {
        Student student = new Student();
        try {
            student.method(-10);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}
class Student {
    private int id;
    public void method(int a) throws Exception {
        if (a > 0) {
            System.out.println("a =" + a);
        }else {
//            throw new RuntimeException("输入的值非法");
            throw new Exception("输入的值非法");
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44863237/article/details/128898913