zoukankan      html  css  js  c++  java
  • 嵌套的Try-Catch块--------异常处理(3)

    当有一个Try块没有一个对应的异常可处理,则其父类的异常处理机制去处理。如果父类的异常处理机制不能处理,则java run-time system将会抛出一个异常。

    例子:

    class Nest{
       public static void main(String args[]){
    	 //Parent try block
         try{
        	//Child try block1
             try{
                System.out.println("Inside block1");
                int b =45/0;
                System.out.println(b);
             }
             catch(ArithmeticException e1){
                System.out.println("Exception: e1");
             }
             //Child try block2
             try{
                System.out.println("Inside block2");
                int b =45/0;
                System.out.println(b);
             }
             catch(ArrayIndexOutOfBoundsException e2){
                System.out.println("Exception: e2");
             }
            System.out.println("Just other statement");
        }
        catch(ArithmeticException e3){
        	 System.out.println("Arithmetic Exception");
             System.out.println("Inside parent try catch block");
        }
        catch(ArrayIndexOutOfBoundsException e4){
        	System.out.println("ArrayIndexOutOfBoundsException");
             System.out.println("Inside parent try catch block");
        }
        catch(Exception e5){
        	System.out.println("Exception");
             System.out.println("Inside parent try catch block");
         }
         System.out.println("Next statement..");
      }
    }
    输出:
    Inside block1
    Exception: e1
    Inside block2
    Arithmetic Exception
    Inside parent try catch block
    Next statement..


  • 相关阅读:
    Linux 下杀毒可用工具 clamav
    Docker 添加环境系统文件配置
    Docker 空间大小设置
    Docker 扩容 容器空间大小
    bzoj 1088 DP
    bzoj 1096 斜率优化DP
    spoj p104 Matrix-Tree定理
    bzoj 1016 深搜
    WC后记
    bzoj 1301 后缀数组
  • 原文地址:https://www.cnblogs.com/yandufeng/p/5009626.html
Copyright © 2011-2022 走看看