企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Java 中的`try-catch` - 异常处理 > 原文: [https://beginnersbook.com/2013/04/try-catch-in-java/](https://beginnersbook.com/2013/04/try-catch-in-java/) 在[上一篇教程](https://beginnersbook.com/2013/04/java-exception-handling/)中,我们讨论了什么是异常处理以及我们为什么这样做。在本教程中,我们将看到用于异常处理的`try-catch`块。 ## `try`块 `try`块包含可以发生异常的一组语句。`try`块后面总是跟一个`catch`块,它处理相关`try`块中发生的异常。`try`块必须后跟`catch`块或`finally`块或两者。 ### `try`块的语法 ```java try{ //statements that may cause an exception } ``` > 在编写程序时,如果您认为程序中的某些语句可以抛出异常,请将它们包含在`try`块中并处理该异常 ## `catch`块 `catch`块是处理异常的地方,此块必须遵循`try`块。单个`try`块可以有几个与之关联的`catch`块。您可以在不同的`catch`块中捕获不同的异常。当`try`块中发生异常时,将执行处理该特定异常的相应`catch`块。例如,如果在`try`块中发生算术异常,则执行`catch`块中用于算术异常的语句。 ### java 中`try-catch`的语法 ```java try { //statements that may cause an exception } catch (exception(type) e(object))‏ { //error handling code } ``` ## 示例:`try-catch`块 如果`try`块中发生异常,则执行控制将传递给相应的`catch`块。单个`try`块可以有多个与之关联的`catch`块,您应该放置`catch`块,使得通用异常处理程序`catch`块位于最后(参见下面的示例)。 通用异常处理程序可以处理所有异常但是你应该放在最后,如果你把它放在所有`catch`块之前,那么它将显示通用消息。您始终希望为每种类型的异常提供有意义的消息,而不是通用消息。 ```java 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."); } } ``` 输出: ```java You should not divide a number by zero I'm out of try-catch block in Java. ``` ## Java 中的多个`catch`块 我们在上面看到的示例是有多个`catch`块,让我们在示例的帮助下看到关于多个`catch`块的一些规则。要详细阅读,请参阅[在 java 中捕获多个异常](https://beginnersbook.com/2013/05/catch-multiple-exceptions/)。 1. 如上所述,单个`try`块可以包含任意数量的`catch`块。 2. 通用`catch`块可以处理所有异常。无论是`ArrayIndexOutOfBoundsException`还是`ArithmeticException`或`NullPointerException`或任何其他类型的异常,它都会处理所有这些异常。要查看`NullPointerException`和`ArrayIndexOutOfBoundsException`的示例,请参阅以下文章:[异常处理示例程序](https://beginnersbook.com/2013/04/exception-handling-examples/)。 ```java catch(Exception e){   //This catch block catches all the exceptions } ``` > 如果你想知道为什么我们需要其他捕获处理程序,当我们有一个可以处理所有的通用。这是因为在通用异常处理程序中,您可以显示消息,但您不确定它可能触发的异常类型,因此它将为所有异常显示相同的消息,并且用户可能无法理解发生了哪个异常。这就是你应该放置的原因是在所有特定异常`catch`块的末尾 3. 如果`try`块中没有异常,则完全忽略`catch`块。 4. 对应的`catch`块执行特定类型的异常: `catch(ArithmeticException e)`是一个可以解决`ArithmeticException`的`catch`块 `catch(NullPointerException e)`是一个可以处理`NullPointerException`的`catch`块 5. 你也可以抛出异常,这是一个高级主题,我在单独的教程中介绍了它:[用户定义异常](https://beginnersbook.com/2013/04/user-defined-exception-in-java/),[`throw`关键字](https://beginnersbook.com/2013/12/throws-keyword-example-in-java/),[`throw` vs `throws`](https://beginnersbook.com/2013/04/difference-between-throw-and-throws-in-java/)。 ### 多个`catch`块的示例 ```java 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..."); } } ``` 输出: ```java Warning: ArithmeticException Out of try-catch block... ``` 在上面的示例中,有多个`catch`块,当`try`块中发生异常时,这些`catch`块按顺序执行。这意味着如果你把最后一个`catch`块(`catch(Exception e)`)放在第一个地方,就在`try`块之后,那么在任何异常的情况下,这个块将执行,因为它可以处理所有异常。该挡块应放在最后,以避免这种情况。 ## `finally`块 我在这里单独介绍了这个:[java `finally`块](https://beginnersbook.com/2013/04/java-finally-block/)。现在你只需知道这个块执行是否发生异常。您应该将这些语句放在`finally`块中,必须执行是否发生异常。