Java面向对象基础--异常处理(try catch throw throws finally)

Java面向对象基础–异常处理(try catch throw throws finally)

异常不是错误,是在运行过程中代码产生的一种例外。

所有的异常类是从 java.lang.Exception 类继承的子类。
Exception 类是 Throwable 类的子类。除了Exception类外,Throwable还有一个子类Error 。
Error 用来指示运行时环境发生的错误。
在这里插入图片描述
常见异常类型:
在这里插入图片描述

关键字:try catch throw throws finally

try catch finally

  • 使用 try 和 catch 关键字可以捕获异常。try/catch 代码块放在异常可能发生的地方。
  • 发生异常时,直接进入catch
  • finally 关键字用来创建在 try 代码块后面执行的代码块。无论是否发生异常,finally 代码块中的代码总会被执行。
  • 一个try 代码块后面跟随多个 catch 代码块的情况就叫多重捕获。多重catch最后一个异常一定是exception。

例1:

public class Tset11 {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            int number=5/0;
            System.out.println(number);

            String aaa=null;
            System.out.println(aaa.length());
        }catch (ArithmeticException e){
    
    
            System.out.println("除数不能为0!!!");
        }catch (NullPointerException e){
    
    
            System.out.println("值为空");
        }
        finally {
    
    
            System.out.println("这个无论如何都要执行!");
        }
    }
}

代码分析:
当第一段代码报错时,会直接进入catch进行判断,看其符合哪个异常,然后输出此异常,最后执行finally
运行结果如下:
在这里插入图片描述
例2:

public class Tset11 {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            int number=6/2;
            System.out.println(number);

            String aaa=null;
            System.out.println(aaa.length());
        }catch (ArithmeticException e){
    
    
            System.out.println("除数不能为0!!!");
        }catch (NullPointerException e){
    
    
            System.out.println("值为空");
        }
        finally {
    
    
            System.out.println("这个无论如何都要执行!");
        }
    }
}

代码分析:
当第一段代码可以执行时,会执行第二段代码,进而报错执行catch,找到相对应的异常执行,最后执行finally
代码运行结果:
在这里插入图片描述
例3、嵌套try-catch

public class Test12 {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            try {
    
    
                int number=6/0;
                System.out.println(number);
            }catch (NullPointerException e){
    
    
                System.out.println("值为空");
            }
        }catch (ArithmeticException e){
    
    
            System.out.println("除数不能为零");
        }catch (Exception e){
    
    
            System.out.println(e);
        }
        finally {
    
    
            System.out.println("这个无论如何都要执行!年轻人不讲武德");
        }
    }
}

代码分析:
try-catch嵌套和if嵌套一样,本例代码在内部try-catch并没有发现异常,所以会执行外部try-catch,最后执行finally
运行结果如下:
在这里插入图片描述

throw throws

如果一个方法没有捕获到一个检查性异常,那么该方法必须使用 throws 关键字来声明,跟在方法后。也可以使用 throw 关键字抛出一个异常,无论它是新实例化的还是刚捕获到的。

public class Test13 extends Exception{
    
    

    public Test13() {
    
    
    }

    public Test13(String message) {
    
    
        super(message);
    }
}
public class Test13_1 extends Test13{
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            show();
        } catch (Test13 test13) {
    
    
            test13.printStackTrace();
        }
    }

    public static void show() throws Test13{
    
    
        if (2>1){
    
    
            throw new Test13("哈哈哈哈哈");
        }
    }
}

代码分析:
自定义一个异常,只有在满足条件的时候才会输出

代码实现如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43928469/article/details/110226960