Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Exceptions and Errors are together known as Throwables. The Throwables are positioned like so in the Object hierarchy:

Errors are fatalities that we would normally want to cause a program failure, while Exceptions are events that we would normally want to handle in our program. An example of using them with a try-catch statement, a 'try' clause followed by a 'catch' clause:

A common idiom for asserting for exceptions is:

Some common exceptions associated with Groovy:

We can put code within a 'finally' clause following a matching 'try' clause, so that regardless of whether the code in the 'try' clause throws an exception, the code in the finally clause will always execute:

We can attach more than one 'catch' clause to a 'try' clause, and attach a 'finally' clause also:

With the multi catch block (since Groovy 2.0), we’re able to define several exceptions to be catch and treated by the same catch block.

An exception will ripple up through the nested blocks, executing only code in 'finally' clauses, until caught, or the thread terminates.

Exceptions will also ripple through function and method invocations

We can mark a function or method indicating what type of Exception it might throw. This is a useful documentation feature:

  • No labels