Java study notes: Java's exception mechanism

Java study notes: Java's exception mechanism

Two processing methods:

  • try-catch
  • throws
    throws thrown exception must be a subclass of throwable

try-catch statement

try
{
    语句1//如果语句1有异常,则语句2不会执行,直接到catch
    语句2//放入可能异常的句子
}
catch(Exception e)   //catch (错误类型 + 对象),                  
{                    //错误类型一般写Exception就可以了,因为Exception是其他错误类型的父类
    //处理或者提醒用户的句子
}
//可以捕捉多个异常
catch(异常类型1 e)    
{
    //...
}
catch(异常类型2 e)
{
    //...
}
...
finally
{
    //无论是否捕捉到,都一定会执行的代码
    //一般用于关闭文件,删除临时文件等资源的清理工作
}

In the above program, only one catch is executed or none of all catch () {} is executed
. There can be no code between catch and catch.

  • Matters needing attention
    First catch the subclass exception, then catch the parent class exception, otherwise compile error, because the parent class exception includes all subclass exceptions.
class A extends Exception {
}

class B extends A {
}

class C extends B {
}

class M {
    public void compare(int i, int j) throws A, B {
        if (i > j)
            throw new A();
        else
            throw new B();
    }
}

public class TestTryCatch {
    public static void main(String[] args) {
        M mm = new M();
        try {
            mm.compare(-4, 1);
        } catch (B bb) {
            System.out.println("左边不能小于右边");
        } catch (A aa) {
            System.out.println("左边不能大于右边");
        }

    }
}

When the subclass overrides the base class method, the scope of the exception thrown by the
subclass method cannot be greater than the exception scope thrown by the base class method Cannot throw exceptions other than base class methods

//自定义异常A
class A extends Exception
{
}

//自定义异常B
class B extends Exception
{
}

//自定义异常C
class C extends Exception
{
}

class M
{
    void f() throws A, B
    {
    }
}

class N extends M
{
 //可以throws A或B,也可以throws A,B  也可以不throws,
 //但不可以throws C  即"子类覆盖了基类方法时,
 //子类方法抛出异常的范围不能大于基类方法抛出的异常范围"
    void f() throws A,B 
    {
    }
}

class Test
{
    public static void main(String[] args)
    {
        M m = new M();
        N n = new N();

        System.out.println("1111");
    }
}
  • Abnormal handler summary

    1. When an error occurs in a Java program, the system automatically detects the error and immediately generates an exception object corresponding to the error.
    2. Then provide the exception object to the JVM.
    3. The JVM will first find the corresponding code to handle the exception. If it is not found, the JVM will do some simple processing and the program will be forcibly terminated.
    4. Programmers can write their own code to catch and handle corresponding exceptions.

  • supplement:

try
{
    //...
}
catch(Exception e)
{
    e.printStackTrace();
}
printStackTrace

Print the error information and path of the error object e in the stack (that is, print out the error information)

YES
Published 7 original articles · Like 8 · Visits 133

Guess you like

Origin blog.csdn.net/weixin_44983848/article/details/105508407
Recommended