exception

  • try和catch可以捕获异常然后让程序继续运行下去?(异常这一块内容学的少,没全弄明白)
package com.exception.demo01;
//Error and Exception
public class Demo01 {
    public static void main(String[] args) {
        //Exception in thread "main" java.lang.StackOverflowError
        //new Demo01().a();

        //Exception in thread "main" java.lang.ArithmeticException: / by zero
        //System.out.println(10/0);

        System.out.println();
    }

    public void a() {
        b();
    }
    public void b() {
        a();
    }
}
package com.exception.demo01;
//捕获和抛出异常
public class Test {
    public static void main(String[] args) {
        try {
            new Test().test(1,0);
        } catch (ArithmeticException e) {
            throw new RuntimeException(e);//对象
        }
        //System.out.println("11");
        //System.out.println("11");
        //System.out.println("11");
    }
    ///假设这方法中,处理不了这个异常,方法上抛出异常
    public void test(int a,int b) throws ArithmeticException
    {
        if (b == 0){
            throw new ArithmeticException();//主动的抛出异常,一般在方法中使用
        }
        System.out.println(a/b);
        //System.out.println("11");
    }





    public void a() {
        b();
    }
    public void b() {
        a();
    }
}
/*  main方法中
        int a = 1;
        int b = 0;
        //编写多重catch语句块注意事项:
        //顺序问题:先小后大,即先子类后父类
        try{    //try监控区域
            if (b == 0){
                throw new ArithmeticException();
            }
            System.out.println(a/b);
        }catch(Error c){   //catch捕获异常
            System.out.println("Error");
        }catch(Exception c){
            System.out.println("Exception");
        }catch(Throwable c){
            System.out.println("Throwable");
        }finally{    //处理善后工作
            System.out.println("finally");
        }//finally 可以不要finally,可用于假设IO,资源,关闭!
        //Exception in thread "main" java.lang.ArithmeticException: / by zero

*/
package com.exception.demo01;
//捕获和抛出异常
public class Test2 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        //ctrl+alt+t
        try {
            System.out.println(a/b);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

        }
    }
}
package com.exception.demo02;
//自定义的异常类
public class MyException extends Exception{
    private int detail;

    public MyException(int a) {
        this.detail = a;
    }
    //toString:异常的打印信息
    @Override
    public String toString() {
        return "MyException{" + "detail=" + detail + '}';
    }
}
package com.exception.demo02;
//自定义异常
public class Test {
    //可能会存在异常的方法
    public static void test(int a) throws MyException
    {
        System.out.println("传递的参数为:"+ a);
        if (a > 10){
            throw new MyException(a);
        }
        System.out.println("OK");
    }

    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            //增加一些处理异常的代码
            System.out.println("MyException=>"+e);
        }
    }
}
posted @   uque  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示