Sample classes.

Java To Go!
Exception handling using the VISITOR pattern
by Chi-keong Iong
Listing 1. Sample classes.


class A
{
    public void method()
            throws Exception
    {
        B b = new B();
        try
        {
            b.method();
        }
        // Implicit Accept
        catch (VisitorException ve)
        {
            // Callback
            ve.visit(this);
        }
        finally { }
    }
}

class B
{
    public void method()
            throws Exception
    {
        C c = new C();
        try
        {
            c.method();
        }
        // Implicit Accept
        catch (VisitorException ve)
        {
            // Callback
            ve.visit(this);
        }
        finally { }
    }
}

class C
{
    public void method()
            throws Exception
    {
        try
        {
            throw new
                VisitorException(this);
        }
        // Implicit Accept
        catch (VisitorException ve)
        {
            // Callback
            ve.visit(this);
        }
        finally { }
    }
}

class TestVisitorException
{
    public void method()
            throws Exception
    {
        A a = new A();
        try
        {
            a.method();
        }
        // implicit Accept
        catch (VisitorException ve)
        {
            // Callback
            ve.visit(this);
        }
        finally { }
    }
    static void main(String[] args)
        throws Exception
    {
        TestVisitorException tve =
            new TestVisitorException();
        tve.method();
    }
}

Featured

Upcoming Events

AppTrends

Sign up for our newsletter.

I agree to this site's Privacy Policy.