Tags

,

Groovy being a JVM language, handling exception in groovy is similar to java. In groovy exceptions are optional. In Groovy exception will be passed to the calling method until it is handled, more importantly the targeted exception need not be defined in method signature. There isn’t any difference between handling checked and unchecked exceptions in groovy. Even checked exception need not be defined in the method signature. Which means developers will have the liberty to handle it when they need to, rather than in every hierarchy.

To explain this concept, let us take an example. Consider an application which manages the lifecycle of Album (Music Album). There is an Album class as expressed in the code below

package groovy.samples.exceptionhandling

class Album {
	def name
	def artist
	def tracks = []
}

There is an AlbumManager class which is expected have the lifecycle method(s) to manage the CRUD on Album object. I have only expressed the createAlbum method intended to be used in this example.

package groovy.samples.exceptionhandling

class AlbumManager {

	def createAlbum(def name, def artist, def trackList) {
		if (name == "" || name == null) {
			throw new InvalidAlbumNameException("Album name should be valid and cannot be empty")
		}
		new Album(name: name, artist: artist, tracks: trackList)
	}
}

As you can see, I have createAlbum method which takes as input the required parameters to create an Album but, it does not have any throws clause as in the case of java. Even though it does not have a throws clause, it does throw an InvalidAlbumNameException based on some validation. It might sound strange to have a null check validation inside a business method. But you can forgive me for this time, as I am intended to explain only the exception handling mechanism here.

Now, let us go the client which calls this method. I have written a junit TestCase AlbumManagerTest.

package groovy.sample.exceptionhandling.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import groovy.samples.exceptionhandling.AlbumManager
import groovy.samples.exceptionhandling.InvalidAlbumNameException;
import junit.framework.TestCase;

class AlbumManagerTest {
	@Test
	def void testCreateAlbum() {
		AlbumManager albumManager = new AlbumManager()
		try {
			albumManager.createAlbum("", "Micheal Jackson", ["Jam", "Why you wanna trip on me", "Heal the world", "Black or White", "Remember the Time", "Dangerous"])
		} catch (e) {
			println e
			assert e in InvalidAlbumNameException
		}		
	}
}

Although there isn’t an throws clause in which an exception is defined in createAlbum method of AlbumManager, I have catch the exception in the testcase and handled it. I am handling all the exceptions here, though there is possibility to catch specific exception. If you want to catch all exception in one shot, in groovy it is enough to give only the object on which you are going to receive the Exception like catch( e ). Just to assure the test case is passed, I have asserted the received exception against InvalidAlbumNameException. To make sure we are running through the expected catch block, I have given a println inside the catch block.

So, the idea here is, in Groovy it is not necessary to declare throws clause explicitly (even for checked exceptions). Groovy takes care of passing the exceptions to the caller. Incase if the caller wants to handle the exception, can define specific or generic exception blocks to handle it. Other exception handling mechanisms stand good for groovy as in java like, multiple exception handling inside single catch block or defining multiple catch block for each kind of exception etc.

Though, there is one point that you might need to remember. If you are working on application that is being developed based on grails framework, you might need to define the throws clause explicitly. This is needed because, grails perform object initiation from one layer to another layer (say controller to service) through Spring Injection. The mechanism is not pure Groovy, even though your end objects are Groovy. So, it is necessary to define the exception in throws clause for this situation (as like in java).

Source available in my github public repo for groovy and grails

Thanks for reading this blog. Hope it should be useful to you.

Advertisements