Java 异常

异常

Error和Exception

Java把异常当作对象处理,并定义一个基类java.lang.Throwable作为所有异常的超类

异常类分为两大类:Error错误和Exception异常

Error通常是灾难性的致命的错误,是程序无法控制和处理的,当出现这些异常时,JAVA虚拟机(JVM)一般会选择终止线程

Exception通常情况下可以被程序处理的,并且在程序中应该尽可能的去处理这些异常。

捕获和抛出异常

异常处理的五个关键字:try catch finally throw throws

public class Test {
    public static void main(String[] args) {
        int a=1;
        int b=0;

        try{ //监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){//catch 捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        }finally {//处理善后工作
            System.out.println("finally");
        }
        
        //fianlly 可以不要,假设IO,资源,需要关闭,可以把关闭的操作放在finally里面

    }
}    
public class Test {
    public static void main(String[] args) {
        int a=1;
        int b=0;

        //假设要捕获多个异常,要从小到大

        try{ //监控区域

            if(b==0){//主动抛出异常 throw  throws
                throw new ArithmeticException();
            }

            System.out.println(a/b);
        }catch (Error e){//catch(想要捕获的异常类型) 捕获异常
            System.out.println("Error");
        }catch (Exception n){
            System.out.println("Exception");
        }catch(Throwable t){
            System.out.println("Throwable");
        }
        finally {//处理善后工作
            System.out.println("finally");
        }

        //fianlly 可以不要,假设IO,资源,需要关闭,可以把关闭的操作放在finally里面


        try {
            new Test().text(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } finally {
        }

    }

    //假设这个方法中处理不了这个异常,方法上抛出异常 throws
    public void text(int a,int b) throws ArithmeticException{
        if(b==0){//主动抛出异常 throw  throws
            throw new ArithmeticException();//主动抛出异常,一般在方法中使用
        }

        System.out.println(a/b);

    }
}
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) {
            System.exit(0);
            e.printStackTrace();//打印错误的栈信息
        } finally {

        }

    }
}

自定义异常

用户自定义异常类,只需要继承Exception类即可

  1. 创建自定义异常类
  2. 在方法中通过throw关键字抛出异常对象
  3. 如果在当前抛出异常的方法中处理异常,可以使用try-catch语句捕获处理,否则在方法的声明处通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作
  4. 在出现异常方法的调用者中捕获并处理异常
//自定义的异常类
public class MyException extends Exception{

    //传递数字  >10;
    private int detail;

    public MyException(int a) {
        this.detail=a;
    }

    //toString:异常的打印信息

    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}
public class Test03 {
    //可能会存在异常的方法
    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);
        }
    }
}

经验总结:

  • 处理运行时异常,采用逻辑规避同时辅助try-catch处理
  • 在多重catch块后面,可以加一个catch(Exception)来处理可能遗漏的异常
  • 对于不确定的代码,可以加上try-catch,处理潜在的异常
  • 尽量的去处理异常,切记只简单的调用printStackTrace()去打印输出
  • 尽量添加finally语句块去释放占用的资源
posted @   十几里路  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示