multiple

103 views
Skip to first unread message

njw

unread,
Dec 22, 2015, 5:10:54 PM12/22/15
to gosu-lang
Hello. I am trying to catch multiple exceptions in a single catch block to avoid duplicate code. The following example illustrates the problem and solution in java. Does gosu support this functionality?

catch (IOException ex) {
     logger.log(ex);
     throw ex;
catch (SQLException ex) {
     logger.log(ex);
     throw ex;
}

In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable ex has different types.

The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:

catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
}

Thank you,
Nathan

Kyle Moore

unread,
Dec 22, 2015, 6:01:42 PM12/22/15
to gosu...@googlegroups.com
Hi Nathan, and thanks for using Gosu.

Unfortunately we don't support the multi-catch syntax introduced in Java 7. Your code, in Gosu, would have to look like this:

try {
foo = new Foo()
}
catch(ex : IOException) {
//handle or throw
}
catch(ex : SQLException) {
//handle or throw
}
catch(ex : Exception) {
//handle or ??
}
finally {
//...
}
Note that we can reuse the variable name since its scope is limited to the catch block declaring it.

Of course, if you would like to log an enhancement request on our Github home, we are happy to consider adding multi-catch: https://github.com/gosu-lang/gosu-lang/issues

- Kyle


--
You received this message because you are subscribed to the Google Groups "gosu-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gosu-lang+...@googlegroups.com.
To post to this group, send email to gosu...@googlegroups.com.
Visit this group at https://groups.google.com/group/gosu-lang.
For more options, visit https://groups.google.com/d/optout.

njw

unread,
Dec 23, 2015, 10:07:29 AM12/23/15
to gosu-lang
Hello Kyle,

Thank you for your quick response. I will return with more gosu related questions as needed. Be back soon.

Nathan

Reply all
Reply to author
Forward
0 new messages