Take the situation when you are writing a library for objects designed to run asynchronously. To notify the caller of the status of an asynchronous object, the caller must implement a listener interface.
Now that you are using listeners, it is likely that the listener will do something wrong, which will throw an exception for the library object.
Example
public interface SomeListener { public void progressNotification(int status); } public class SomeClass { SomeListener listener = null; âĶ public void setListener(SomeListener listener) { this.listener = listener; } âĶ public void someRandomMethod() { int progressStatus = 0;
If SomeClass does not catch the exception and does not handle it, this will result in any code after listener.progressNotification(progressStatus); will not be executed if the object is in the "wrong" state.
So my question is: what's the best way to handle this exception in the library?
I saw one library that does this:
if (listener != null) { try { listener.progressNotification(progressStatus); }catch (Thrwowable th) { System.err.println("An uncaught throwable " + th); } }
which doesn't suit me.
source share