1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java捕获异常和抛出异常_Java捕获多个异常 重新抛出异常

java捕获异常和抛出异常_Java捕获多个异常 重新抛出异常

时间:2019-11-12 19:30:23

相关推荐

java捕获异常和抛出异常_Java捕获多个异常 重新抛出异常

java捕获异常和抛出异常

In Java 7, catch block has been improved to handle multiple exceptions in a single catch block. If you are catching multiple exceptions and they have similar code, then using this feature will reduce code duplication. Let’s understand java catch multiple exceptions feature with an example.

在Java 7中,对catch块进行了改进,可以在单个catch块中处理多个异常。 如果捕获多个异常并且它们具有相似的代码,则使用此功能将减少代码重复。 让我们通过一个示例来了解Java捕获多个异常功能。

Java捕获多个异常 (Java catch multiple exceptions)

Before Java 7, we used to catch multiple exceptions one by one as shown below.

在Java 7之前,我们曾经一一捕获多个异常,如下所示。

catch (IOException ex) {logger.error(ex);throw new MyException(ex.getMessage());catch (SQLException ex) {logger.error(ex);throw new MyException(ex.getMessage());}

In Java 7, we can catch both these exceptions in a single catch block as:

在Java 7中,我们可以在单个catch块中捕获这两个异常,如下所示:

catch(IOException | SQLException ex){logger.error(ex);throw new MyException(ex.getMessage());}

If a catch block handles multiple exceptions, you can separate them using a pipe (|) and in this case, exception parameter (ex) is final, so you can’t change it. The byte code generated by this feature is smaller and reduce code redundancy.

如果catch块处理多个异常,则可以使用管道(|)分隔它们,在这种情况下,异常参数(ex)是最终的,因此您无法更改它。 此功能生成的字节代码较小,并减少了代码冗余。

Java重新抛出异常 (Java rethrow exception)

Another improvement is done in Compiler analysis of rethrown exceptions. Java rethrow exception allows you to specify more specific exception types in the throws clause of a method declaration.

编译器对重新抛出的异常进行分析的另一项改进。 Java rethrow异常允许您在方法声明的throws子句中指定更特定的异常类型。

Let’s see this with a small example:

让我们来看一个小例子:

package com.journaldev.util;public class Java7MultipleExceptions {public static void main(String[] args) {try{rethrow("abc");}catch(FirstException | SecondException | ThirdException e){//below assignment will throw compile time exception since e is final//e = new Exception();System.out.println(e.getMessage());}}static void rethrow(String s) throws FirstException, SecondException,ThirdException {try {if (s.equals("First"))throw new FirstException("First");else if (s.equals("Second"))throw new SecondException("Second");elsethrow new ThirdException("Third");} catch (Exception e) {//below assignment disables the improved rethrow exception type checking feature of Java 7// e=new ThirdException();throw e;}}static class FirstException extends Exception {public FirstException(String msg) {super(msg);}}static class SecondException extends Exception {public SecondException(String msg) {super(msg);}}static class ThirdException extends Exception {public ThirdException(String msg) {super(msg);}}}

As you can see that inrethrowmethod, catch block is catching Exception but it’s not part of throws clause. Java 7 compiler analyze the complete try block to check what types of exceptions are thrown and then rethrown from the catch block.

如您所见,在rethrow方法中,catch块正在捕获Exception,但它不是throws子句的一部分。 Java 7编译器分析完整的try块,以检查引发了哪些类型的异常,然后将其从catch块中抛出。

Note that this analysis is disabled if you change the catch block argument.

请注意,如果更改catch块参数,则将禁用此分析。

Further Reading: Exception Handling in Java.

进一步阅读: Java中的异常处理 。

翻译自: /629/java-catch-multiple-exceptions-rethrow-exception

java捕获异常和抛出异常

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。