Super Simple Seam Catch Example

Jason Porter gave me this code, a rock-bottom simplified Seam Catch example. I construct it and inch along. My goal is to be able to set breakpoints in the handlers and hit them. I poured this into Eclipse, then added a main() method to run it:

Initial run of example

MyClass.java:
package com.jboss.jasonporter.simple;

import javax.enterprise.event.Event;
import javax.inject.Inject;

import org.jboss.seam.exception.control.ExceptionToCatch;

public class MyClass
{
    @Inject
    Event< ExceptionToCatch >    catchEvent;

    public void myMethod()
    {
        try
        {
            throw new Exception( "Uh, oh, out of ideas" );
        }
        catch( Exception e )
        {
            catchEvent.fire( new ExceptionToCatch( e ) );
        }
    }

    public static void main( String[] args )
    {
        MyClass    mc = new MyClass();

        mc.myMethod();
    }
}

Debug as Java Application:

In myMethod I hit line 17 and threw the exception, then line 21 where I check what's in the exception (exactly what's expected):

        cause: java.lang.Exception
detailMessage: Uh, oh, out of ideas
   stackTrace: null

Running to completion, we see:


Exception in thread "main" java.lang.NullPointerException
    at com.jboss.jasonporter.simple.MyClass.myMethod(MyClass.java:21)
    at com.jboss.jasonporter.simple.Main.main(Main.java:9)

The NullPointerException is the real problem—what we'd like to handle ultimately in our Seam Catch mechanism.




Add guts into example

Now let's complete the example with some code behind the event/catch mechanims.

As I see the null pointer exception, I think to handle it through Seam Catch. First, I construct an enumeration that notes such a handler as well as a generic one (purely out of completeness?) for Exception:

package com.jboss.jasonporter.simple;

public enum SimpleSeamCatchEnum
{
    NULLPOINTEREXCEPTION ( new NullPointerException( "Null pointer thrown!" ) ),
    EXCEPTION            ( new Exception( "Generic exception thrown" ) );

    private final Throwable    exception;
    private SimpleSeamCatchEnum( final Throwable e ) { this.exception = e; }

    public Throwable getException() { return exception; }
}

Then, I turn my attention to the handers and handler output:

StandardHandlerDeclaration.java:
package com.jboss.jasonporter.simple;

import java.util.ResourceBundle;

import org.jboss.seam.exception.control.CaughtException;
import org.jboss.seam.exception.control.Handles;
import org.jboss.seam.exception.control.HandlesExceptions;
import org.jboss.seam.exception.control.TraversalMode;

import com.jboss.jasonporter.simple.HandlerOutput;

@HandlesExceptions
public class StandardHandlerDeclaration
{
    final ResourceBundle    messages = ResourceBundle.getBundle( "com.jboss.jasonporter.simple.messages" );

    // superstition makes me put this in; is it relevant as this example stands?
    public void throwableHandler( @Handles( during = TraversalMode.BREADTH_FIRST ) CaughtException< Throwable > event )
    {
        HandlerOutput.printToConsole( this.messages, event.getException(), "throwableHandler", "markHandled" );
        event.unmute();
        event.markHandled();
    }

    public void nullPointerHandler( @Handles CaughtException< NullPointerException > event )
    {
        // * * * * * * * * * * * * * * * * * * * * * * * *
        // HERE'S WHERE I'D LIKE TO STOP IN THE DEBUGGER!!
        // * * * * * * * * * * * * * * * * * * * * * * * *
        HandlerOutput.printToConsole( this.messages, event.getException(), "nullPointerHandler", "handled" );
        event.handled();
    }

    public void exceptionHandler( @Handles CaughtException< Exception > event )
    {
        HandlerOutput.printToConsole( this.messages, event.getException(), "exceptionHandler", "rethrow" );
        event.rethrow();
    }
}
HandlerOutput.java:
package com.jboss.jasonporter.simple;

import java.text.MessageFormat;
import java.util.ResourceBundle;


public class HandlerOutput
{
    public static void printToConsole( final ResourceBundle messages,
                                       final Throwable exception,
                                       final String handler,
                                       final String markException )
    {
        final String output = MessageFormat.format( messages.getString( "output_format" ),
                                                    exception.getClass(),
                                                    handler,
                                                    markException,
                                                    exception.getMessage() );
        System.out.println( output );
    }
}
messages.properties:
output_format=Seam Catch: class({0}), handler name={1}, handled state={2}, exception detail={3}

At this point, my project is more or less finished and appears thus:

Debug as Java Application.

Now I set my breakpoint (at line 30 in StandardHandlerDeclaration.java) and step through. The result is exactly the same as execution never wanders into the handler and hits the desired breakpoint.



Conclusion

This is some wiring that I don't yet understand. Setting a breakpoint in NavigationServlet's HandlerOutput methods never resulted in any different behavior than this example. I imagine one of the following as culprit: