异常

异常

1.Error

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

  • JAVA虚拟机运行错误(Virtual MachineError),当JVM不再有继续执行操作所需的内存资源时,将出现OutOfMemoryError。

    这些异常发生时,Java虚拟机(JVM)一般会选择线程终止。

  • 还有发生在虚拟机试图执行应用时,如类定义错误(NoClassDefFoundError)、链接错误(LinkageError)。

    这些错误是不可查的,因为它们在应用程序的控制和处理能力之外,而且绝大多数是程序运行时不允许出现的情况。

2.Exception

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

    • ArrayIndexOutOfBoundsException(数组下标越界)

    • NullPointerException(空指针异常)

    • ArithmeticException(算术异常)

    • MissingResourceException(丢失资源)

    • ClassNotFoundException(找不到类)等异常,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。

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

  • Error和Exception的区别:Error通常是灾难性的致命的错误,是程序无法控制和处理的,当出现这些异常时,java虚拟机(jvm)

    一般会选择终止线程;Exception通常情况下是可以被程序处理的,并且在程序中应该尽可能地去处理这些异常。

3.异常处理机制

  • 抛出异常

  • 捕获异常

  • 异常处理五个关键字:

    try、catch、finally、throw、throws

    引用笔记

throw和throws作为Java中两种异常抛出关键字,虽然两个长的很像,但是却有着很大的区别。
区别1:
throws:
    跟在方法声明后面,后面跟的是异常类名

throw:
   用在方法体内,后面跟的是异常类对象名  
1
2
3
4
5
public static void method() throws ArithmeticException {// 跟在方法声明后面,后面跟的是异常类名
       int a=10;
       int b=0;
       if(b==0) {
      throw new ArithmeticException();用在方法体内,后面跟的是异常类对象名
      }else {
      System.out.println(a/b);
      }
}
}
1
2
3
4
5
6
7
8
9
10
区别2:
throws:
   可以跟多个异常类名,用逗号隔开

throw:
  只能抛出一个异常对象名
1
2
3
4
5
public static void method() throws ArithmeticException,Exception {//跟多个异常类名,用逗号隔开
       int a=10;
       int b=0;
       if(b==0) {
      throw new ArithmeticException();// 只能抛出一个异常对象名
      }else {
      System.out.println(a/b);
      }
}
}
1
2
3
4
5
6
7
8
9
10
区别3:
throws:
    表示抛出异常,由该方法的调用者来处理

throw:
   表示抛出异常,由该方法体内的语句来处理
1
2
3
4
5

public class throwandthrows {
public static void main(String[] args) {
try {
method();//由该方法的调用者来处理
}catch (ArithmeticException e) {
  e.printStackTrace();
}
}

public static void method() throws ArithmeticException {
       int a=10;
       int b=0;
       if(b==0) {
      throw new ArithmeticException();//由该方法体内的语句来处理
      }else {
      System.out.println(a/b);
      }
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
区别4:
throws:
    throws表示有出现异常的可能性,并不一定出现这些异常

throw:
   throw则是抛出了异常,执行throw一定出现了某种异常
1
2
3
4
5
我们向上面例子代码里throws一个IndexOutOfBoundsException异常,编译发现并没有报错,这就体现了throws表示有出现异常的可能性


public class throwandthrows {
public static void main(String[] args) {
try {
method();
}catch (ArithmeticException e) {
  e.printStackTrace();
}
}

public static void method() throws ArithmeticException,IndexOutOfBoundsException {
       int a=10;
       int b=0;
       if(b==0) {
      throw new ArithmeticException();
      }else {
      System.out.println(a/b);
      }
}
}
ublic class Test1 {
   public static void main(String[] args) {
       try {
           new Test1().t(3,0);
      } catch (ArithmeticException e) {
           e.printStackTrace();
           //在命令行打印异常信息在程序中出错的位置及原因
      }
  }
   //假设在方法中,处理不了这个异常,方法上抛出异常
   public void t(int a,int b) throws ArithmeticException{
       if(b==0){
           throw new ArithmeticException();//主动抛出异常,一般在方法中使用
      }
  }
}
package exception;

public class Test {
   public static void main(String[] args) {
       int a=1;
       int b=0;
       //Ctrl+Alt+T 快捷键
       //假设要捕获多个异常,从小到大~

       try{ //try监控区域
           System.out.println(a/b);
      }catch (ArithmeticException e) {//catch捕获异常 参数():想要捕获的异常类型 最高类型Throwable
           System.out.println("exception");
      }catch(Error e){
           System.out.println("Error");
      }catch(Throwable t){
           System.out.println("Throwable");
      }finally {//finally处理善后工作
           System.out.println("finally");
      }

       //finally 可以不要finally,假设IO、资源,关闭!
  }
}

 

posted @ 2021-09-26 14:11  阿林学习笔记  阅读(114)  评论(0)    收藏  举报