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

     

    小结:

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

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

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

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

  • 相关阅读:
    如何实线浏览器title的小图标以及rgb和rgbaopacity的区别
    雪碧图
    定位的使用及页面的布局
    标准文档流及浮动的元素和坏处
    三种列表以及行级块三种元素的转变
    背景的处理
    h5前期js知识点10月19日总结
    h5前期jg知识点总结10月18日
    h5前期js10月17日知识点
    h5前期js知识点10月16日总结
  • 原文地址:https://www.cnblogs.com/liuhongfeng/p/4173232.html
Copyright © 2011-2022 走看看