个性化阅读
专注于IT技术分析

Java捕获多个异常

点击下载

本文概述


Java多捕获块

一个try块之后可以是一个或多个catch块。每个catch块必须包含一个不同的异常处理程序。因此, 如果必须在发生不同的异常时执行不同的任务, 请使用java multi-catch块。

要记住的要点

  • 一次只发生一个异常, 一次只执行一个catch块。
  • 必须将所有catch块从最具体到最一般的顺序进行排序, 即, 对于ArithmeticException的catch必须先于对Exception的catch。

例子1

让我们看一下Java多捕获块的简单示例。

public class MultipleCatchBlock1 {

	public static void main(String[] args) {
		
		   try{  
			    int a[]=new int[5];  
			    a[5]=30/0;  
			   }  
			   catch(ArithmeticException e)
		          {
				   System.out.println("Arithmetic Exception occurs");
				  }  
			   catch(ArrayIndexOutOfBoundsException e)
		          {
				   System.out.println("ArrayIndexOutOfBounds Exception occurs");
				  }  
			   catch(Exception e)
		          {
				   System.out.println("Parent Exception occurs");
				  }  		  
			   System.out.println("rest of the code");  
	}
}

立即测试

输出:

Arithmetic Exception occurs
rest of the code

例子2

public class MultipleCatchBlock2 {

	public static void main(String[] args) {
		
		   try{  
			    int a[]=new int[5];  
			  
			    System.out.println(a[10]);
			   }  
			   catch(ArithmeticException e)
		          {
				   System.out.println("Arithmetic Exception occurs");
				  }  
			   catch(ArrayIndexOutOfBoundsException e)
		          {
				   System.out.println("ArrayIndexOutOfBounds Exception occurs");
				  }  
			   catch(Exception e)
		          {
				   System.out.println("Parent Exception occurs");
				  }  		  
			   System.out.println("rest of the code");  
	}
}

立即测试

输出:

ArrayIndexOutOfBounds Exception occurs
rest of the code

例子3

在此示例中, try块包含两个异常。但是一次只能发生一个异常, 并且会调用其相应的catch块。

public class MultipleCatchBlock3 {

	public static void main(String[] args) {
		
		   try{  
			    int a[]=new int[5];  
			    a[5]=30/0;  
			    System.out.println(a[10]);
			   }  
			   catch(ArithmeticException e)
		          {
				   System.out.println("Arithmetic Exception occurs");
				  }  
			   catch(ArrayIndexOutOfBoundsException e)
		          {
				   System.out.println("ArrayIndexOutOfBounds Exception occurs");
				  }  
			   catch(Exception e)
		          {
				   System.out.println("Parent Exception occurs");
				  }  		  
			   System.out.println("rest of the code");  
	}
}

立即测试

输出:

Arithmetic Exception occurs
rest of the code

例子4

在此示例中, 我们生成NullPointerException, 但未提供相应的异常类型。在这种情况下, 将调用包含父异常类Exception的catch块。

public class MultipleCatchBlock4 {

	public static void main(String[] args) {
		
		   try{  
			    String s=null;
			    System.out.println(s.length());
			   }  
			   catch(ArithmeticException e)
		          {
				   System.out.println("Arithmetic Exception occurs");
				  }  
			   catch(ArrayIndexOutOfBoundsException e)
		          {
				   System.out.println("ArrayIndexOutOfBounds Exception occurs");
				  }  
			   catch(Exception e)
		          {
				   System.out.println("Parent Exception occurs");
				  }  		  
			   System.out.println("rest of the code");  
	}
}

立即测试

输出:

Parent Exception occurs
rest of the code

例子5

让我们看一个示例, 在不保持异常顺序的情况下处理异常(即从最具体到最一般)。

class MultipleCatchBlock5{  
  public static void main(String args[]){  
   try{  
    int a[]=new int[5];  
    a[5]=30/0;  
   }  
   catch(Exception e){System.out.println("common task completed");}  
   catch(ArithmeticException e){System.out.println("task1 is completed");}  
   catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");}  
   System.out.println("rest of the code...");  
 }  
}

立即测试

输出:

Compile-time error
赞(0)
未经允许不得转载:srcmini » Java捕获多个异常

评论 抢沙发

评论前必须登录!