Java学习-Exception又分为checked异常和unchecked异常,请分别举例说明。

Java学习-Exception又分为checked异常和unchecked异常,请分别举例说明。,第1张

Java学习-Exception又分为checked异常和unchecked异常,请分别举例说明。
  • 受检异常(checked exception):
    编译时需要检查的异常,需要用try-catch或throws处理。

  • 受检异常(unchecked exception):
    不需要在编译时处理的异常。在java中派生于Error和RuntimeException的异常都是unchecked exception。

  • 受检异常举例:

    class A{
       public  static void main(String[]args){
           try(Scanner sc=new Scanner(System.in)){
               int a=sc.nextShort();
           }catch (Exception e){
               e.printStackTrace();
           }
       }
    }
    
  • 非受检异常举例:

    class A{
        public  static void main(String[]args){
            int []a;
            System.out.println(a[0]);
        }
    }
    

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4691397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-07
下一篇 2022-11-07

发表评论

登录后才能评论

评论列表(0条)

保存