zoukankan      html  css  js  c++  java
  • java中的Checked Exception和Unchecked Exception的区别

    Java 定义了两种异常:

      - Checked exception: 继承自 Exception 类是 checked exception。代码需要处理 API 抛出的 checked exception,要么用 catch 语句,要么直接用 throws 语句抛出去。

      - Unchecked exception: 也称 RuntimeException,它也是继承自 Exception。但所有 RuntimeException 的子类都有个特点,就是代码不需要处理它们的异常也能通过编译,所以它们称作 unchecked exception。RuntimeException(运行时异常)不需要try...catch...或throws 机制去处理的异常。

    NullpointerException 的继承级别。 

     

    NullpointerException 继承自 RuntimeException,所以它是个 unchecked exception。

     最常用的五种RuntimeException:    

     ArithmeticException

    int a=0;
    int b= 3/a;

     ClassCastException:

    Object x = new Integer(0);
    System.out.println((String)x);

     IndexOutOfBoundsException
        ArrayIndexOutOfBoundsException,
        StringIndexOutOfBoundsException 

    int [] numbers = { 1, 2, 3 };
    int sum = numbers[3];

    IllegalArgumentException
        NumberFormatException

    int a = Interger.parseInt("test");

    NullPointerExceptionextends

     

    小结:

    检查性异常: 不处理编译不能通过

    非检查性异常:不处理编译可以通过,如果有抛出直接抛到控制台。

    运行时异常: 就是非检查性异常

    非运行时异常: 就是检查性异常

  • 相关阅读:
    增长思维——模式&&组织
    BackUP
    增长思维——机会
    Android
    增长思维——作战地图
    Server架构 小知识
    Server
    产品思维——创新模式
    产品思维——用户体验
    博客迁移到~http://zhulingyu.com
  • 原文地址:https://www.cnblogs.com/liuhongfeng/p/4173232.html
Copyright © 2011-2022 走看看