Java异常处理

 

仿照写出异常捕获语句

Class exp3_4{

   Public static void main(String [] args){

      Int [] a = {1, 2, 3};

      String b = “我爱西邮”;

 

      Try{

         For(int I=0; i<10; i++){

System.out.println(“数组 a 中第 ”+i+” 数值是” + a[i]);

         }

      }catch(Exception e){

         System.out.println(e.toString());

      }

      //仿照写出异常捕获语句

For(int I=0; i<10; i++){

         System.out.println(“字符串 b 中第 ”+i+” 字符是” + b.charAt(i));

      }

   }

}

 

改后:

class exp3_1{

   public static void main(String [] args){

      int [] a = {1, 2, 3};

      String b = "我爱西邮";

 

      try{

         for(int i=0; i<10; i++){

System.out.println("数组 a 中第 "+i+" 数值是" + a[i]);

         }

      }catch(Exception e){

         System.out.println(e.toString());

      }

      try{

         

             for(int i=0; i<10; i++){

         System.out.println("字符串 b 中第 "+i+" 字符是" + b.charAt(i));

      }

      }catch(Exception e){

         System.out.println(e.toString());

         }

   }

}

因为后面没有继续添加其他运行语句:

所以两者没有差别

如果是下例则不同:

如:

原来:

class exp3_4{

   public static void main(String [] args){

      int [] a = {1, 2, 3};

      String b = "我爱西邮";

 

      try{

         for(int i=0; i<10; i++){

System.out.println("数组 a 中第 "+i+" 数值是" + a[i]);

         }

      }catch(Exception e){

         System.out.println(e.toString());

      }

      //仿照写出异常捕获语句

for(int i=0; i<10; i++){

         System.out.println("字符串 b 中第 "+i+" 字符是" + b.charAt(i));

      }

      System.out.println("hello");//注意:此处加了输出语句

   }

}

 Java异常处理

 

加了异常处理后:

class exp3_4{

   public static void main(String [] args){

      int [] a = {1, 2, 3};

      String b = "我爱西邮";

 

      try{

         for(int i=0; i<10; i++){

System.out.println("数组 a 中第 "+i+" 数值是" + a[i]);

         }

      }catch(Exception e){

         System.out.println(e.toString());

      }

      //仿照写出异常捕获语句

      try{

     

for(int i=0; i<10; i++){

         System.out.println("字符串 b 中第 "+i+" 字符是" + b.charAt(i));

      }

      }catch(Exception e){

         System.out.println(e.toString());

      System.out.println("hello");

   }

}

}

 Java异常处理

 

:注意:加了异常处理后输出hello

 

 

 

你可能感兴趣的:(杂七杂八)