Throwable、Error、Exception、RuntimeException 的区别

详解Java异常Throwable、Error、Exception、RuntimeException的区别


在Java中,根据错误性质将运行错误分为两类:错误和异常。
在Java程序的执行过程中,如果出现了异常事件,就会生成一个异常对象。生成的异常对象将传递Java运行时系统,这一异常的产生和提交过程称为抛弃(throw)异常。
当Java运行时系统得到一个异常对象时,它将会沿着方法的调用栈逐层回溯,寻找处理这一异常的代码。找到能够处理这类异常的方法后,运行时系统把当前异常对象交给这个方法进行处理,这一过程称为捕获(catch)异常。


1. Throwable类是 Java 语言中所有错误或异常的超类。它的两个子类是Error和Exception;


2. Error是Throwable 的子类,用于指示合理的应用程序不应该试图捕获的严重问题。大多数这样的错误都是异常条件。虽然   ThreadDeath 错误是一个“正规”的条件,但它也是 Error 的子类,因为大多数应用程序都不应该试图捕获它。在执行该方法期间,无需在其 throws 子句中声明可能抛出但是未能捕获的 Error 的任何子类,因为这些错误可能是再也不会发生的异常条件。

Error类包括一些严重的程序不能处理的系统错误类,如内存溢出、虚拟机错误、栈溢出等。这类错误一般与硬件有关,与程序本身无关,通常由系统进行处理,程序本身无法捕获和处理。

1)OutOfMemoryError内存溢出一般是出现在申请了较多的内存空间没有释放的情形。

  1. //java.lang.OutOfMemoryError  -Xmx150m  
  2. try {  
  3.     byte[] b = new byte[1024*1024*600];  
  4. catch (OutOfMemoryError e) {  
  5.     e.printStackTrace();  
  6. }  
//java.lang.OutOfMemoryError  -Xmx150m
try {
    byte[] b = new byte[1024*1024*600];
} catch (OutOfMemoryError e) {
    e.printStackTrace();
}

运行时,设置jvm最大的heap内存150m,此时申请600m的内存,因此会报error

  1. java.lang.OutOfMemoryError: Java heap space  
java.lang.OutOfMemoryError: Java heap space

2)java.lang.StackOverflowError
堆栈溢出错误。当一个应用递归调用的层次太深而导致堆栈溢出时抛出该错误。

  1. public static void main(String[] args) {  
  2.     method();  
  3. }  
  4.   
  5. public static void method() {  
  6.     while (true) {  
  7.         method();  
  8.     }  
  9. }  
public static void main(String[] args) {
    method();
}

public static void method() {
    while (true) {
        method();
    }
}
无限次的递归调用出现

  1. Exception in thread “main” java.lang.StackOverflowError  
Exception in thread "main" java.lang.StackOverflowError


3. Exception类及其子类是 Throwable 的一种形式,它指出了合理的应用程序想要捕获的条件。
有些异常在编写程序时无法预料的,如中断异常、非法存取异常等。为了保证程序的健壮性,Java要求必须对这些可能出现的异常进行捕获,并对其进行处理。


4. RuntimeException类是Exception类的子类。RuntimeException是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类。可能在执行方法期间抛出但未被捕获的RuntimeException 的任何子类都无需在 throws 子句中进行声明。它是Exception的子类。

常见的运行时异常:

  1. try {  
  2.     String str = new String(“AA”);  
  3.     str = null;  
  4.     System.out.println(str.length());  
  5. catch (NullPointerException e) {  
  6.      e.printStackTrace();  
  7.     System.out.println(”出现空指针的异常了”);  
  8. }  
  9.   
  10.   
  11. try {  
  12.     Object obj = new Date();  
  13.     String str = (String) obj;  
  14. catch (ClassCastException e) {  
  15.     System.out.println(”出现类型转换的异常了”);  
  16. catch (Exception e) {  
  17.     e.printStackTrace();  
  18. finally {  
  19.     System.out.println(”处理完异常后的逻辑”);  
  20. }  
  21.   
  22.   
  23. try {  
  24.     int i = 10;  
  25.     System.out.println(i / 0);  
  26. catch (ArithmeticException e) {  
  27.     System.out.println(”算术异常”+e.getMessage());  
  28. }  
  29.   
  30.   
  31.   
  32. try {  
  33.     int[] i = new int[10];  
  34.     System.out.println(i[-10]);  
  35. catch (ArrayIndexOutOfBoundsException e) {  
  36.     System.out.println(”数组下标越界的异常!”);  
  37. }  
try {
    String str = new String("AA");
    str = null;
    System.out.println(str.length());
} catch (NullPointerException e) {
     e.printStackTrace();
    System.out.println("出现空指针的异常了");
}


try {
    Object obj = new Date();
    String str = (String) obj;
} catch (ClassCastException e) {
    System.out.println("出现类型转换的异常了");
} catch (Exception e) {
    e.printStackTrace();
} finally {
    System.out.println("处理完异常后的逻辑");
}


try {
    int i = 10;
    System.out.println(i / 0);
} catch (ArithmeticException e) {
    System.out.println("算术异常"+e.getMessage());
}



try {
    int[] i = new int[10];
    System.out.println(i[-10]);
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("数组下标越界的异常!");
}

5. IOExeption类是Exception类的子类。
从一个不存在的文件中读取数据
越过文件结尾继续读取

连接一个不存在的URL

  1. FileInputStream fis = null;  
  2. try {  
  3.     fis = new FileInputStream(new File(“hello1.txt”));  
  4.     int b;  
  5.     while ((b = fis.read()) != -1) {  
  6.         System.out.print((char) b);  
  7.     }  
  8.   
  9. catch (FileNotFoundException e1) {  
  10.     System.out.println(”文件找不到了!”);  
  11. catch (IOException e1) {  
  12.     System.out.println(e1.getMessage());  
  13. finally {  
  14.     try {  
  15.         fis.close();  
  16.     } catch (IOException e) {  
  17.         e.printStackTrace();  
  18.     }  
  19. }  
FileInputStream fis = null;
try {
    fis = new FileInputStream(new File("hello1.txt"));
    int b;
    while ((b = fis.read()) != -1) {
        System.out.print((char) b);
    }

} catch (FileNotFoundException e1) {
    System.out.println("文件找不到了!");
} catch (IOException e1) {
    System.out.println(e1.getMessage());
} finally {
    try {
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}



猜你喜欢

转载自blog.csdn.net/heatdeath/article/details/80286165