在上一篇教程中,我们讨论了什么是异常处理以及我们为什么这样做。在本教程中,我们将看到用于异常处理的try-catch块。
try块
try块包含可以发生异常的一组语句。try块后面总是跟一个catch块,它处理相关try块中发生的异常。try块必须后跟catch块或finally块或两者。
try块的语法
try{//statements that may cause an exception}
在编写程序时,如果您认为程序中的某些语句可以抛出异常,请将它们包含在
try块中并处理该异常
catch块
catch块是处理异常的地方,此块必须遵循try块。单个try块可以有几个与之关联的catch块。您可以在不同的catch块中捕获不同的异常。当try块中发生异常时,将执行处理该特定异常的相应catch块。例如,如果在try块中发生算术异常,则执行catch块中用于算术异常的语句。
java 中try-catch的语法
try{//statements that may cause an exception}catch (exception(type) e(object)){//error handling code}
示例:try-catch块
如果try块中发生异常,则执行控制将传递给相应的catch块。单个try块可以有多个与之关联的catch块,您应该放置catch块,使得通用异常处理程序catch块位于最后(参见下面的示例)。
通用异常处理程序可以处理所有异常但是你应该放在最后,如果你把它放在所有catch块之前,那么它将显示通用消息。您始终希望为每种类型的异常提供有意义的消息,而不是通用消息。
class Example1 {public static void main(String args[]) {int num1, num2;try {/* We suspect that this block of statement can throw* exception so we handled it by placing these statements* inside try and handled the exception in catch block*/num1 = 0;num2 = 62 / num1;System.out.println(num2);System.out.println("Hey I'm at the end of try block");}catch (ArithmeticException e) {/* This block will only execute if any Arithmetic exception* occurs in try block*/System.out.println("You should not divide a number by zero");}catch (Exception e) {/* This is a generic Exception handler which means it can handle* all the exceptions. This will execute if the exception is not* handled by previous catch blocks.*/System.out.println("Exception occurred");}System.out.println("I'm out of try-catch block in Java.");}}
输出:
You should not divide a number by zeroI'm out of try-catch block in Java.
Java 中的多个catch块
我们在上面看到的示例是有多个catch块,让我们在示例的帮助下看到关于多个catch块的一些规则。要详细阅读,请参阅在 java 中捕获多个异常。
- 如上所述,单个
try块可以包含任意数量的catch块。 - 通用
catch块可以处理所有异常。无论是ArrayIndexOutOfBoundsException还是ArithmeticException或NullPointerException或任何其他类型的异常,它都会处理所有这些异常。要查看NullPointerException和ArrayIndexOutOfBoundsException的示例,请参阅以下文章:异常处理示例程序。
catch(Exception e){//This catch block catches all the exceptions}
如果你想知道为什么我们需要其他捕获处理程序,当我们有一个可以处理所有的通用。这是因为在通用异常处理程序中,您可以显示消息,但您不确定它可能触发的异常类型,因此它将为所有异常显示相同的消息,并且用户可能无法理解发生了哪个异常。这就是你应该放置的原因是在所有特定异常
catch块的末尾
- 如果
try块中没有异常,则完全忽略catch块。 - 对应的
catch块执行特定类型的异常:
catch(ArithmeticException e)是一个可以解决ArithmeticException的catch块
catch(NullPointerException e)是一个可以处理NullPointerException的catch块 - 你也可以抛出异常,这是一个高级主题,我在单独的教程中介绍了它:用户定义异常,
throw关键字,throwvsthrows。
多个catch块的示例
class Example2{public static void main(String args[]){try{int a[]=new int[7];a[4]=30/0;System.out.println("First print statement in try block");}catch(ArithmeticException e){System.out.println("Warning: ArithmeticException");}catch(ArrayIndexOutOfBoundsException e){System.out.println("Warning: ArrayIndexOutOfBoundsException");}catch(Exception e){System.out.println("Warning: Some Other exception");}System.out.println("Out of try-catch block...");}}
输出:
Warning: ArithmeticExceptionOut of try-catch block...
在上面的示例中,有多个catch块,当try块中发生异常时,这些catch块按顺序执行。这意味着如果你把最后一个catch块(catch(Exception e))放在第一个地方,就在try块之后,那么在任何异常的情况下,这个块将执行,因为它可以处理所有异常。该挡块应放在最后,以避免这种情况。
finally块
我在这里单独介绍了这个:java finally块。现在你只需知道这个块执行是否发生异常。您应该将这些语句放在finally块中,必须执行是否发生异常。
