File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Application Frameworks and the fly likes Seam and Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Seam and Exception Handling" Watch "Seam and Exception Handling" New topic

Seam and Exception Handling

Kuladip Yadav
Ranch Hand

Joined: Jul 30, 2008
Posts: 162

Hi Dan ,

Is there any special module or functionality for exception handing
in Seam ?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Yes, I believe. Seam reference - Chapter 5. Events, interceptors and exception handling
Dan Allen
Ranch Hand

Joined: Mar 05, 2003
Posts: 164
Exception handling is one of the areas were Seam truly improves JSF. Lack of an exception handling facility in JSF was a huge oversight. According to the spec, you were expected to handle exceptions in web.xml. There are two problems with this approach. The first is that exceptions were caught outside of the JSF context. Second was that all you can do is direct the user to an error page, with no opportunity to close database connections, rollback transactions, or any other cleanup you might need to do.

Seam takes both a declarative and event-based approach to exception handling. In the Seam page descriptor (i.e., /WEB-INF/pages.xml), you define any number of <exception> elements, which allow you to declaratively control exception handling. Using that element you can catch an exception and either redirect to an error page with an optional message or send an HTTP error code with optional message.

In Seam 2.1, you can have a message logged.

Another option is to annotate a custom exception with @Redirect or @HttpError, giving you the same control as the page descriptor for redirecting to an error page or sending an http error code, respectively.

@Redirect(viewId = "/tooManyBeers.xhtml", message = "You are cut off!")
com.mojavelinux.connoissuer.TooManyBeersException extends Exception {

By default, Seam will rollback an active transaction when an exception is raised, as well as perform some other internal cleanup work. You can control which exceptions cause a rollback (i.e., disable the default behavior) using the @ApplicationException annotation:

The exception handlers (both XML and annotations) allow you to control whether or not the current long-running conversation should be ended.

That covers the declarative solution. Seam's exception handling facility also keeps observers in the loop. When an exception is captured, Seam raises the org.jboss.seam.exceptionHandled event and when the exception falls through, Seam raises the org.jboss.seam.exceptionNotHandled event.

Keep in mind when testing your exception handlers that if you are running Facelets in development mode, the debug page might trap the exception before it gets to the Seam exception handler.

Dan Allen | | Author of Seam in Action -
I agree. Here's the link:
subject: Seam and Exception Handling
It's not a secret anymore!