groovy
  1. groovy
  2. GROOVY-1984

Shorther syntax for catching multiple exceptions at once

    Details

    • Number of attachments :
      2

      Description

      John Wilson suggested this improved syntax for catching several exceptions at once:

      This has been suggested for Java
      http://members.capmac.org/~orb/blog.cgi/tech/java/Better_exception_sy.html

      I would have thought it's trivial to do in Groovy

      try { 
          Field field = someClass.getDeclaredField(someField); 
          someValue = field.getInt(Barcode.class); 
      } catch (SecurityException, NoSuchFieldException e) { 
          // do something else with the exception 
      }
      

        Activity

        Guillaume Laforge made changes -
        Field Original Value New Value
        Description John Wilson suggested this improved syntax for catching several exceptions at once:

        This has been suggested for Java
        http://members.capmac.org/~orb/blog.cgi/tech/java/Better_exception_sy.html

        I would have thought it's trivial to do in Groovy

        try {
            Field field = someClass.getDeclaredField(someField);
            someValue = field.getInt(Barcode.class);
        } catch (SecurityException, NoSuchFieldException e) {
            // do something else with the exception
        }
        John Wilson suggested this improved syntax for catching several exceptions at once:

        This has been suggested for Java
        http://members.capmac.org/~orb/blog.cgi/tech/java/Better_exception_sy.html

        I would have thought it's trivial to do in Groovy
        {code}
        try {
            Field field = someClass.getDeclaredField(someField);
            someValue = field.getInt(Barcode.class);
        } catch (SecurityException, NoSuchFieldException e) {
            // do something else with the exception
        }
        {code}
        Hide
        Guillaume Laforge added a comment -

        See the thread on the mailing-list for further suggestions:
        http://www.nabble.com/Multiple-exception-types-in-a-single-catch-block-tf4086071.html

        Show
        Guillaume Laforge added a comment - See the thread on the mailing-list for further suggestions: http://www.nabble.com/Multiple-exception-types-in-a-single-catch-block-tf4086071.html
        Hide
        Aaron Digulla added a comment -

        The catch statement doesn't look quite right. On one hand, there must be only a single "e", OTOH, we have two types. On the ML, there have been two suggestions:

        } catch (SecurityException | NoSuchFieldException e) { 
        

        and

        } catch (SecurityException, NoSuchFieldException) {
           // this is a closure; you can find the exception in "it"
        }
        

        Personally, I like the closure idea very much, especially when I can define the closure elsewhere! That would allow to override the default error handling which opens a whole new world of ways to deal with errors.

        As for "|" vs. ",": The bar would express the "OR"-iness of the statement better but IMHO, it's just a list of types and lists use ",". Also, comma is easier to type.

        Show
        Aaron Digulla added a comment - The catch statement doesn't look quite right. On one hand, there must be only a single "e", OTOH, we have two types. On the ML, there have been two suggestions: } catch (SecurityException | NoSuchFieldException e) { and } catch (SecurityException, NoSuchFieldException) { // this is a closure; you can find the exception in "it" } Personally, I like the closure idea very much, especially when I can define the closure elsewhere! That would allow to override the default error handling which opens a whole new world of ways to deal with errors. As for "|" vs. ",": The bar would express the "OR"-iness of the statement better but IMHO, it's just a list of types and lists use ",". Also, comma is easier to type.
        Hide
        Paul King added a comment -
        Show
        Paul King added a comment - See also: http://www.javac.info/Multicatch.html
        Hide
        Guillaume Laforge added a comment -

        Setting the fix version to 1.8, to adapt to the multicatch proposal for JDK / Java 7

        Show
        Guillaume Laforge added a comment - Setting the fix version to 1.8, to adapt to the multicatch proposal for JDK / Java 7
        Guillaume Laforge made changes -
        Fix Version/s 1.8-beta-x [ 15750 ]
        Fix Version/s 2.0 [ 13489 ]
        Hide
        CÚdric Champeau added a comment -

        Here's a patch which adds support for Java 7 style multicatches :

        try {
        ...
        } catch (IOException | NullPointerException e) {
        ...
        }
        

        It translates this as a sequence of catch blocks with the same body.

        All unit tests pass. Note that I had some troubles with the SourcePrinter, and I'm not sure I'm doing things correctly at that place. Feel free to review.

        Show
        CÚdric Champeau added a comment - Here's a patch which adds support for Java 7 style multicatches : try { ... } catch (IOException | NullPointerException e) { ... } It translates this as a sequence of catch blocks with the same body. All unit tests pass. Note that I had some troubles with the SourcePrinter , and I'm not sure I'm doing things correctly at that place. Feel free to review.
        CÚdric Champeau made changes -
        Guillaume Laforge made changes -
        Fix Version/s 1.9-beta-1 [ 17153 ]
        Fix Version/s 1.8.x [ 15750 ]
        Hide
        Guillaume Laforge added a comment -

        For reference, the Project Coin JSR details about the multicatch exception can be found here:
        http://jcp.org/aboutJava/communityprocess/edr/jsr334/index.html

        Show
        Guillaume Laforge added a comment - For reference, the Project Coin JSR details about the multicatch exception can be found here: http://jcp.org/aboutJava/communityprocess/edr/jsr334/index.html
        Guillaume Laforge made changes -
        Assignee Cédric Champeau [ melix ]
        Guillaume Laforge made changes -
        Status Open [ 1 ] Closed [ 6 ]
        Resolution Fixed [ 1 ]
        Hide
        CÚdric Champeau added a comment -

        Reopening to provide a patch for the "final" modifier not being supported

        Show
        CÚdric Champeau added a comment - Reopening to provide a patch for the "final" modifier not being supported
        CÚdric Champeau made changes -
        Resolution Fixed [ 1 ]
        Status Closed [ 6 ] Reopened [ 4 ]
        Hide
        CÚdric Champeau added a comment -

        The following patch fixes the "final" modifier issue by supporting the syntax, but without giving any semantics to it. See http://groovy.329449.n5.nabble.com/This-is-a-new-bug-td4258524.html

        Adds tests cases too.

        Show
        CÚdric Champeau added a comment - The following patch fixes the "final" modifier issue by supporting the syntax, but without giving any semantics to it. See http://groovy.329449.n5.nabble.com/This-is-a-new-bug-td4258524.html Adds tests cases too.
        CÚdric Champeau made changes -
        Attachment Support_for_final_modifier.patch [ 54353 ]
        Guillaume Laforge made changes -
        Status Reopened [ 4 ] Closed [ 6 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            CÚdric Champeau
            Reporter:
            Guillaume Laforge
          • Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: