zoukankan      html  css  js  c++  java
  • try,catch,finally尝试(一个程序块多个catch)


    曾学过c++,但是对这些异常捕捉不是很了解,通过别的编程语言了解

    public class newclass {
    
    public static void main(String[] args) {
    try {int [] arr=new int[5];
    arr[1]=0;
    //    arr[10]=1;
    arr[0]=3;
    int res=arr[0]/arr[1];
    }
    catch(ArrayIndexOutOfBoundsException ex2) {
    System.out.println("2");
    
    ex2.printStackTrace(); }
    catch(ArithmeticException ex1){
    System.out.println("1");
    
    ex1.printStackTrace(); 
    
    }
    catch(Exception ex){ex.printStackTrace(); } 
    finally{
    System.out.println("不存在的");}
    
    System.out.println("为什么急着");}
    
    }

    总结

    一个程序块多个catch只会处理符合的那个,其他自定义的catch异常不会处理,而数组越界是编译器最早发现的,所以最早抛出错误,所以是2.

  • 相关阅读:
    git 常用命令
    最近任务 && react文章列表
    markdown 常用格式API
    webpack 插件怎么写
    iframe
    重置浏览器滚动条css
    webpack4
    vscode插件开发
    vscode插件
    react router 的push replace
  • 原文地址:https://www.cnblogs.com/otakus/p/12171808.html
Copyright © 2011-2022 走看看