Handling Java Exceptions in "Events"

I would like to get a second opinion on how to handle exceptions in "events" (key input, screen refresh, etc.). In this case, I have control over the event sender.

Thus, the module is configured to handle the event (it implements the listener interface and registers with the event sender):

public void DefaultSet ( CardData oldDefault, CardData newDefault )
{
}

the event sender is simply:

        for ( Enumeration e = listeners.elements(); e.hasMoreElements(); )
        {
            RetrieverListener thisListener = (RetrieverListener) e.nextElement();
            thisListener.DefaultSet( oldDefault, newDefault );
        }

So, if / when something goes wrong in the receiver:

  • Should I try to deal with this exception and never send anything to the sender? Sometimes listeners do not have a “context” for correctly handling the error, is this true?

  • , , ? " IO reset..". javadocs, .

  • , - , ?

+5
3

Java - , . , RuntimeException, - , , , .

(, ), . ErrorHandler, :

public interface ErrorHandler {
    public void errorOccurred(String whatIWasTryingToDo, Exception failure);
}

ErrorHandler .

public class SomeClass implements SomeKindOfListener 
    private final ErrorHandler errorHandler;
    ... other fields ...

    public SomeClass(ErrorHandler errorHandler, ... other parameters ... ) {
        this.errorHandler = errorHandler;
        ...
    }

    public void listenerCallback(SomeEvent e) {
        try {
            ... do something that might fail ...
        }
        catch (SomeKindOfException e) {
            errorHandler.errorOccurred("trying to wiggle the widget", e);
        }
    }         
}

, , . , , , .

+6

, . - , , , .

+1

- . .

RuntimeException s. . NPE, , . . Error , , OutOfMemeory, . .

0

All Articles