The second exception handling policy.

Java To Go!
Exception handling using the VISITOR pattern
by Chi-keong Iong
Listing 3. The second exception handling policy.


class VisitorException
            extends Exception
{
    // an optional object reference to
    // record the original thrower
    private Object thrower=null;

    public VisitorException()
        { super(); }
    public VisitorException(String s)
        { super(s); }
    public VisitorException(Object o)
        { super();  thrower = o; }
    public VisitorException(String s,
                                Object o)
        { super(s); thrower = o; }

    void visit(A a) throws Exception
    {
        System.out.println(
            “A is visited.”);

        // Throw another
        // exception object!
        throw new RuntimeException();
    }
    void visit(C c) throws Exception
    {
        System.out.println(
            “C is visited.”);
        throw this;         // Rethrow!
    }
    void visit(Object o)
        throws Exception
    {
        // Default behavior for all
        // undefined visited classes
        throw this;         // Rethrow!
    }
}

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.