Analysis of multiple exception examples captured in java 7

Source: Internet
Author: User

Java 7 allows us to capture multiple exceptions in the same catch statement block. This is also called multi-exception capture.


Before Java 7, we may want to do this:

Try {

// Execute code that may throw 1 of the 3 exceptions below.

} Catch (SQLException e ){
Logger. log (e );

} Catch (IOException e ){
Logger. log (e );

} Catch (Exception e ){
Logger. severe (e );
}
As shown above, SQLException and IOException are both handled in the same way, but you still need to write two independent catch statement blocks for these two exceptions.

In java 7, you can catch multiple exceptions as follows:

Try {

// Execute code that may throw 1 of the 3 exceptions below.

} Catch (<strong> SQLException | IOException e </strong> ){
Logger. log (e );

} Catch (Exception e ){
Logger. severe (e );
}
Note: The two exception names in the first catch statement block are separated by pipeline characters. Pipeline characters between two exception classes are the methods for declaring multiple exceptions in the same catch statement block.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.