Exceptions as objects

The traditional convention of sending an integer as an error flag is problematic and unsatisfactory in OOP. The C++ exception handling mechanism can offer much more flexibility, safety and robustness. It’s important to note that an exception is just like any other object: it can have data members and member functions. This gives the exception handler more options for recovery rather than aborting the program. A clever exception object can have a member function which returns a detailed verbal description of the error (instead of letting the handler look it up in some table). It can also offer a rectifying member function, enabling the program to revert. For example, a logger class which appends new records to an existing log file. Whenever it fails to open the log file, it throws an exception object. The corresponding handler can query the exception why it was thrown and react accordingly. Furthermore, a clever exception object can have a member function which creates a dialog box . The operator can gather all the relevant information about the exception and choose recovery measures from the dialog box: create a new log file, redirect the logger to another file, or simply let the system run without a logger.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: