Griffon: Dealing with uncaught exceptions - No Fluff Just Stuff

Griffon: Dealing with uncaught exceptions

Posted by: Andres Almiray on March 22, 2011

How many times have you encountered a rogue exception that bubbles up to the top of the call stack, causing an application to crash and burn? At least a few times, right? Fortunately the JDK provides a handy mechanism for dealing with such exceptions: Thread.UncaughtExceptionHandler. OK, so registering an implementation of such type should be enough to get the application back on track, however you're out of luck if an exception happens inside the EventDispatchThread. You'll need to register the implementation in a different way. More over, unless your design includes some form of multiplexing the handling of the exception, then only one component will be able to handle said exception. Allow me to introduce a different way to handle uncaught exceptions regardless of the place they are thrown.

The Griffon SDK includes a class named GriffonExceptionHandler whose main responsibility is to catch any exceptions that your code did not catch, including those thrown inside the EventDispatchThread. It does so by implementing the Thread.UncaughtExceptionHandler interface and registering itself in the following manner:
This takes care of the first issue but it does not solve the second one. Another responsibility of this class is to propagate events through the Griffon event bus (Hamlet recently posted a screencast on this topic, go check it out!). Now any component that registers itself with the application as an event listener will get a chance to react to these type of exceptions. Let's see a quick example.

Assuming that you have a Griffon application with the following View and Controller classes

BombView.groovy
BombController.groovy
Clicking on the button triggers an exception but the Controller nor the View have code that can handle it. If GriffonExceptionHandler were not automatically registered at boot time then the application would end abruptly as soon after you click that button. Praise goodness that's not the case! But how do we add code that handles the exception? Of course you can add a try/catch block to the controller action but that would defeat the purpose of showing the event bus in action. Every Controller (and Service instance for that matter) in Griffon are registered as application event listeners automatically. We could add an even listener to the Controller, however that would be akin to adding a try/catch in the action, i.e, the component that throws the exception is also the one that handles it. We'll choose a different route and register a global event handler for this case.

Global event handlers are the same as any other event handlers, the only difference is that they are defined in a special script (griffon-app/conf/Events.groovy) or class (src/main/Events.java). Here's a quick way to deal with the event
Assuming that you have a Griffon application with the following View and Controller classes

griffon-app/conf/Events.groovy
This should result in a similar output on your console once you click on the button

Oops, got an unexpected exception bomb.BombException

If you look closer to the output on your console you should also see something akin to this

2011-03-22 12:45:41,157 [Thread-2] ERROR griffon.util.GriffonExceptionHandler - Uncaught Exception
bomb.BombException
    at bomb.BombController$_closure1_closure2.doCall(BombController.groovy:5)
    at bomb.BombController$_closure1_closure2.doCall(BombController.groovy)

That's right, GriffonExceptionHandler will also log the exception for you, it will even filter the stack trace so that you don't see any groovy internals nor java reflection bits. There's more information about this feature at the Griffon Guide, section 5.5.10. With a few more lines of code you can build an event handler that presents a dialog to the user, like this one


I'll show how to get it done on a future post but if you're feeling impatient then jump to Github and clone the sample bomb application.

Keep on Groovying!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »