New JAVA 7 Features--capture multiple exceptions in a single catch block, and re-throw exceptions with the type of upgrade

Source: Internet
Author: User


In Java 7, the catch code block was upgraded to handle multiple exceptions in a single catch block. If you are capturing multiple exceptions and they contain similar code, using this feature will reduce code duplication. Here is an example to understand.

Versions prior to Java 7:

12345678910 catch (IOException ex) {     logger.error(ex);     throw new MyException(ex.getMessage());catch (SQLException ex) {     logger.error(ex);     throw new MyException(ex.getMessage());}catch (Exception ex) {     logger.error(ex);     throw new MyException(ex.getMessage());}
1 在Java 7中,我们可以用一个catch块捕获所有这些异常:
1234 catch(IOException | SQLException | Exception ex){     logger.error(ex);     thrownewMyException(ex.getMessage());}

If you use a catch block to handle multiple exceptions, you can separate them with a pipe (|) character, in which case the exception parameter variable (EX) is defined as final and therefore cannot be modified. This feature will generate fewer bytecode and reduce code redundancy.

Another upgrade is the compiler's handling of the re-throwing exception (Rethrown exceptions). This feature allows more specific exception types to be specified in the throws clause of a method declaration.

Let's take a look at a small example:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 package com.journaldev.util;public class Java7MultipleExceptions {    public static void main(String[] args) {        try{            rethrow("abc");        }catch(FirstException | SecondException | ThirdException e){            //以下赋值将会在编译期抛出异常,因为e是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");            else                throw new ThirdException("Third");        } catch (Exception e) {            //下面的赋值没有启用重新抛出异常的类型检查功能,这是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 in the Rethrow method, the catch block catches an exception that does not appear in the throws clause. The Java 7 compiler parses the complete try code block to check what type of exception is thrown and re-thrown from the catch block.

Note that once you change the parameters of the catch block, the compiler's analysis will not be enabled.

New JAVA 7 Features--capture multiple exceptions in a single catch block, and re-throw exceptions with the type of upgrade

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: