Java07异常

Java07异常

image

异常的概念

image
异常的案例

package com.mingmao.javaexception;

public class ConceptOfException {
    public static void main(String[] args) {
        //调用a
        //new ConceptOfException().a(); //StackOverflowErrorn 栈溢出

        System.out.println(11/0); //ArithmeticException: / by zero 算术异常:/ by 零
    }

    public void a(){
        b();
    }

    public void b(){
        a();
    }
}

image
image
image
image

异常处理机制

image

package com.mingmao.javaexception;

//捕获异常可以使程序即使出现异常也继续执行下去
public class ExceptionHandlingMechanism {
    public static void main(String[] args) {
        int a=1;
        int b=0;

        //System.out.println(a/b); //.ArithmeticException: / by zero

        //捕获异常
        //try...catch必须要有,可以没有finally
        //IO流等等,需要关闭,可有finally执行
        try {//try监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){//如果出现ArithmeticException异常,便执行此代码块内容,捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        }finally {//无论是否出现异常,都执行此代码块,处理善后工作
            System.out.println("finally");
        }


        //new ExceptionHandlingMechanism().a(); //StackOverflowError
        //Throwable 可捕获任何异常
        //可捕获多个异常,可以层层递进
        try {//try监控区域
            new ExceptionHandlingMechanism().a();
        }catch (Error e){//如果出现StackOverflowError异常,便执行此代码块内容,捕获异常
            System.out.println("程序出现异常Error");
        }catch (Exception e){
            System.out.println("程序出现异常,Exception");
        }catch (Throwable e){
            System.out.println("程序出现异常 Throwable");
        } finally {//无论是否出现异常,都执行此代码块,处理善后工作
            System.out.println("finally");
        }

        //生成异常捕获的快捷键:选中代码:Ctrl+Alt+T
        try {
            new ConceptOfException().b();
        } catch (Throwable e) {
            System.out.println("程序出现异常");
        } finally {
            System.out.println("finally");
        }

        //主动抛出异常
        //new ExceptionHandlingMechanism().test(1,0);

        //捕获方法中向上抛出的异常
        try {
            new ExceptionHandlingMechanism().test1(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } finally {
        }

        System.out.println("程序继续。。。");
    }

    public void a(){b();}
    public void b(){a();}

    public void test(int a, int b){
        //主动抛出异常,一般在方法中使用
        if(b==0){
            throw new ArithmeticException();
        }
    }

    //假设在方法里处理不了这个异常,继续向上抛,throws,调用此方法时需要捕获异常
    public void test1(int a, int b) throws ArithmeticException{
        //主动抛出异常,一般在方法中使用
        if(b==0){
            throw new ArithmeticException();
        }
    }
}

自定义异常

image

package com.mingmao.javaexception;

//自定义的异常类
public class UserDefinedException extends Exception{
    //传递数字,数字>10就抛出异常
    private int num;

    public UserDefinedException(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "UserDefinedException{" +
                "num=" + num +
                '}';
    }
}
package com.mingmao.javaexception;

public class Test {
    public static void main(String[] args) {
        try {
            new Test().run(11);
        } catch (UserDefinedException e) {
            System.out.println("UserDefinedException=>"+e);
        } finally {
        }
    }

    public void run(int a) throws UserDefinedException{
        if(a>10){
            throw new UserDefinedException(a);
        }
    }
}

异常总结

image

学习视频

学习视频

posted @ 2021-09-10 17:14  明懋  阅读(28)  评论(0)    收藏  举报