Java中Error和Exception的区别

Error和Exception的区别

在这里插入图片描述

Error

Error类对象由Java虚拟机生成并抛出,大多数错误与代码编写者所执行的
操作无关。

Java虚拟机运行错误(Virtual MachineError),当JVM不再有继续执行操作
所需的内存资源时,将出现OutOfMemoryError。这些异常发生时,Java
虚拟机(JVM)一般会选择线程终止;

还有发生在虚拟机试图执行应用时,如类定义错误(NoClassDefFoundError)、链接错误(LinkageError)。这些错误是不可查的,因为它们在应用程序的控制和处理能力之外,而且绝大多数是程序运行时不允许出现的状况。

Exception

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

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

这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生;

Error和Exception的区别

Error通常是灾难性的致命的错误,是程序无法控制和处理的,当出现这些异常时,Java虚拟机(JVM)一般会选择终止线程;
Exception通常情况下是可以被程序处理的,并且在程序中应该尽可能的去处理这些异常。

异常的处理机制

  • 抛出异常
  • 捕获异常

异常处理五个关键字:try catch finally throw throws
finally 可以不要finally, 但假如是IO,资源,则需在finally中关闭

捕获Exception

public class Demo {
    public static void main(String[] args){
        int a=10;
        int b=0;
        System.out.println(a/b);
    }
}

在这里插入图片描述
解决方法:

public class Demo {
    public static void main(String[] args){
        int a=1;
        int b=0;
        try{//try监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){//catch 捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        }finally {//处理善后工作
            System.out.println("finally");

        }
   //finally   可以不要finally,   假如IO,资源,则需在finally中关闭

    }
}

运行结果:

程序出现异常,变量b不能为0
finally

Process finished with exit code 0

捕获Error

public class Demo {
    public static void main(String[] args){
        new Demo().a();
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}

在这里插入图片描述
解决方法:

public class Demo {
    public static void main(String[] args){
        try{//try监控区域
            new Demo().a();
        }catch (Error e){//catch 捕获异常
            System.out.println("程序出现错误:StackOverflowError");
        }finally {//处理善后工作
            System.out.println("finally");

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

运行结果:

程序出现错误:StackOverflowError
finally

Process finished with exit code 0

在这里插入图片描述

public class Demo {
    public static void main(String[] args){
        int a=10;
        int b=0;
        try {
            System.out.println(a/b);
        }catch (Error e){
            System.out.println("Error");
        }catch (Exception e){
        System.out.println("Exception");
        }catch (Throwable t){
            System.out.println("Throwable");
        }finally {
            System.out.println("finally");
        }
    }
}
Exception
finally

Process finished with exit code 0

在这里插入图片描述

快捷键抛出异常
选中代码:Ctrl+Alt+T
在这里插入图片描述
在这里插入图片描述

throw/throws 主动抛出异常

public class Demo {
    public static void main(String[] args) {
        new Demo().test(1,0);

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

在这里插入图片描述

自定义异常

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

//自定义异常类
public class MyException extends Exception{
    //传递数字>10;
    private int detail;

    public MyException(int a) {
        this.detail = a;
    }
    //toString:异常的打印信息
    @Override
    public String toString() {
        return "MyException{" + detail +
                '}';
    }
}

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) {
            //e.printStackTrace();
            System.out.println("MyException=>"+e);
        }
    }
    }
//运行结果:    
//传递的参数为:9
//OK
//传递的参数为:11
//MyException=>MyException{11}

//e--->toString()

在这里插入图片描述

可提供远程搭建运行服务

不会调试运行的同学,你只需打开远程,会帮你搭建调试好一切(JDK、Idea/Eclipse、MySQL、Tomcat、Maven………)
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一杯奶茶¥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值