Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If you don't really care of the exception which are thrown inside your try block, you can simply catch any of them and simply omit the type of the caught exception. So instead of catching the throwables exceptions like in:

Code Block
try {
    // ...
} catch (ThrowableException t) {
    // something bad happens
}

...

Code Block
try {
    // ...
} catch (any) {
    // something bad happens
}
Info
Note that it's catching all Exceptions, not Throwables. If you need to really catch "everything", you'll have to be explicit and say you want to catch Throwables.

Optional typing advice

I'll finish on some words on when and how to use optional typing. Groovy lets you decide whether you use explicit strong typing, or when you use 'def'.

I've got a rather simple rule of thumb: whenever the code you're writing is going to be used by others as a public API, you should always favor the use of strong typing, it helps making the contract stronger, avoids possible passed arguments type mistakes, gives better documentation, and also helps the IDE with code completion whenever . Whenever the code is for your use only, like private methods, or when the IDE can easily infer the type, then you're more free to decide when to type or not