Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the best practice for handling exceptions thrown from event handlers/lis

ID: 659102 • Letter: W

Question

What is the best practice for handling exceptions thrown from event handlers/listeners in a event loop? For example:

class EventLoop
{
public:
void start(); //create a thread which calls run();
void run()
{
    while(true)
    {
      listener.waitEvent(); //blocks until an event occured
      try
      {
        listener.processEvent(); //calls given handler
      }
      catch(const Exception& excp)
      {
        //The exception is thrown from another class
        //What shall I do?
      }
    }
}
//other code..
}
//Sample event loop usage:
EventLoop el;
SampleListener sampleListener;
el.setListener(sampleListener);
el.start();
//other work..
In this example, when processEvent() throws an exception, the event loop thread should be able to continue to run. Also, the error should be handled. One possible solution may be add an errorOccured() method to listener. In catch block the method could be invoked. But it increases complexity of the program seriously.

Your suggestions? Thanks..

Explanation / Answer

What I do is that I just log the exception class name, the exception message, and the full exception stack trace. I do not think that there is anything else that can be done, nor anything else that needs to be done.

I consider this an integral part of the notifier pattern: the fact that an observer may fail should not affect the notifier in any way whatsoever, nor should it prevent any other observers from observing the notification.

Some people believe that once an exception has been thrown, the application should terminate, but I think that this is terribly short-sighted.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote