异常

error和Exception

Error

  • Error类对象由Java虚拟机生成并抛出,大多数错误与编写代码所执行的操作无关。
  • Java虚拟机运行错误,当JVM不再继续执行所需的内存资源时,将出现OutOfMemoryError。这些异常发生时,Java虚拟机(JVM)一般会选择线程终止;
  • 还有发生在虚拟机试图执行应用时,如类定义错误(NoClassDefFoundError)、链接错误(LinkageError).这些错误是不可能查的,因为他们在应用程序的控制和处理能力之外,而且绝大多数是程序运行时不允许出现的状况。

Exception

1.在Exception分支中有一个重要的子类RuntimeException(运行时异常)

  • ArrayIndexOutOfBoundException(数组下标越界)
  • NullPointerException(空指针异常)
  • ArithmeticException(算术异常)
  • MissingResourceException(丢失资源)
  • ClassNotFoundException(找不到类)等异常,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。

2.这些异常一般是有程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生;
3.Error和Exception的区别:Error通常是灾难性的致命的错误,是程序无法控制和处理的,当出现这些异常时,Java虚拟机(JVM)一般会选择终止线程;Exception通常情况下是可以被程序处理的,并且在程序中应该尽可能的去处理这些异常。

捕获和抛出异常

异常体系结构
在这里插入图片描述
异常处理机制:

捕获异常(try,catch,finally)
抛出异常(方法中throw,方法上throws)

要点

  1. try监控区域
  2. catch(想要捕获的异常类型)捕获异常
  3. finally 处理善后工作
    4.假设要捕获多个异常:从小到大!从上到下!(参考异常体系结构)
  4. 选中异常,ctrl + alt + T,自动生成捕获异常机制,同时在catch中出现语句 e.printStackTrace();//打印错误的栈信息
  5. throw new ArithmeticException();//方法中主动抛出异常
  6. 假设这个方法中,处理不了这个异常。方法上抛出异常:public void test(int a,int b) throws ArithmeticException{}
public class Exception01 {
    public static void main(String[] args) {


        new Exception01().test(1,0);


    }

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


            System.out.println(a / b);
        }
    }


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

   /**     try {//try监控区域




                } catch (Exception e) {
                System.out.println("Exception");
                } catch (Throwable t) {//catch(想要捕获的异常类型)捕获异常
                System.out.println("Throwable");
                } finally {//处理善后工作
                System.out.println("finally");
    }
    **/
public class Exception02 {
    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.out.println("Exception");
            e.printStackTrace();//打印错误的栈信息
        } finally {
            System.out.println("finally");
        }
    }
}

自定义异常

  • 使用Java内置的异常类可以描述在编程时出现的大部分异常情况。除此之外,用户还可以自定义异常。用户自定义异常,只需继承Exception即可。
  • 在程序中使用自定义异常类,大体可以分以下几个步骤:
    1.创建自定义异常类。
    2.在方法中通过throw关键字抛出异常对象
    3.如果当前抛出异常的方法中处理异常,可以使用try-catch语句捕获并处理;否则在方法的声明处通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作。
    4.在出现异常方法的调用者中捕获并处理异常。

要点
1.自定义的异常类public class Exception03 extends Exception{
2.toString:异常的打印信息,@Override//重写方法
public String toString() {
3.测试类编写可能会存在异常的方法,方法中抛出异常,方法上抛出异常,就要主方法中需要捕获异常static void test(int a) throws Exception03 {
4. public static void main(String[] args) {
try {
test(11);
} catch (Exception03 exception03) {
System.out.println(exception03);
}
}
5.方法内捕获,方法上不需要抛出,主方法中也不需要捕获异常。

//自定义的异常类
public class Exception03 extends Exception{

    //传递数字
    private int detail;

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

    //toString:异常的打印信息
    @Override//打印
    public String toString() {
        return "Exception03{" + "datial=" + detail + '}';
    }
}

public class Test03 {

    //可能会存在异常的方法
    static void test(int a) throws Exception03 {

        System.out.println(a);

        if(a>10){
             throw new Exception03(a);//抛出
         }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            test(11);
        } catch (Exception03 exception03) {
            System.out.println(exception03);
        }
    }

}

实际应用中的经验总结

  • 处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理
  • 在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常
  • 对于不确定的代码,也可以加上try-catch,处理潜在的异常
  • 尽量去处理异常,切忌只是简单地调用printStackTrace()去打印输出
  • 具体如何处理异常,要根据不同的业务需求和异常类型来决定
  • 尽量添加finally语句块去释放占用的资源
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值