From the course: Learning Groovy

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Exception handling

Exception handling - Groovy Tutorial

From the course: Learning Groovy

Start my 1-month free trial

Exception handling

- [Instructor] Things can go wrong during the execution of code. Java and groovy use the concept of exceptions to indicate an error condition. The original intent of exceptions is defined as follows. Checked exceptions represent error conditions that a program can recover from and that need to be handled. Unchecked exceptions indicate a failure condition that we can recover from. Handling exceptions in groovy works exactly the same way as in Java. You can write a try block that catches an exception and a catch block that handles the exception. There's one major difference between groovy and Java though. In groovy, exception handling is optional. That means you can decide to handle it or not, even if the exception type is a checked exception. We'll enhance our example code by trying it to convert a string into a long. So here we are going to get the first name. And then we'll turn it into a long and see what happens.…

Contents