TOTD #169: Multi-catch - Using Java 7 in Java EE 6/GlassFish

Project Coin introduces a small set of changes to the Java language making your everyday programming easier. The TOTD #167 explained how to use Automatic Resource Management particularly for a Java EE 6 application. The TOTD #168 showed how to use "String switch statement" for a RESTful Web service. This Tip Of The Day (TOTD) will explain a scenario where multi-catch can be used in Java EE 6 applications.

Consider the following piece of code:

protected void doPost(HttpServletRequest request, HttpServletResponse response){
    PrintWriter out = null;
    try {
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        out.println("<html><head><title>Servlet TestServlet</title></head>");
        out.println("<body>");
        out.println("<h1>Sending email from " + request.getContextPath () + "</h1>");
        
        for (Part p : request.getParts()) {
            // save the parts locally
            System.out.println(p.getName() + " saved");
        }  
       Message message = new MimeMessage(session);        message.setFrom(new InternetAddress(from));        InternetAddress[] address = {new InternetAddress(to)};         message.setRecipients(Message.RecipientType.TO, address);         message.setSubject("File upload successful.");         message.setSentDate(new Date());         message.setText("File has been successfully saved.");         Transport.send(message);
        out.println("</body>");         out.println("</html>");     } catch (ServletException ex) {         Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);     } catch (MessagingException ex) {         Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);     } catch (IOException ex) {         Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);     } finally {         out.close();     } }


If you are familiar with Java EE 6 then you'll understand that this fragment is from a Servlet responding to a POST request. This fragment receives a multi-part message such as a file, saves it locally, and then sends a confirmation email. The fields used for sending an email are initialized in the "init" method of the Servlet (not shown here for brevity).

If you notice then there are 3 "catch" blocks and each one of them is executing the same code, printing a log message. Using JDK7 multi-catch block, the different "catch" blocks can be combined together and rewritten as (highlighted in bold):

    out.println("</body>");
    out.println("</html>");
} catch (ServletException | MessagingException | IOException ex) {
    Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
} finally {            
    out.close();
}

If you have configured NetBeans 7.0.1 for the JDK7 Source/Binary format then it will show you a hint as shown below:

The catch parameter of a multi-catch clause is regarded as implicitly final. An explicit final modifier will be redundant and one more way JDK7 enables more concise syntax.

How are you using JDK7 features in Java EE 6 applications ?

 

Comments:

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today