7

Does anyone know a opensource c++ application with a well designed/robust exception mechanism so I can get some inspiration? Most code/examples I see do questionable things like:

  1. Throw objects with a message string as argument. Seems wrong because it marks the exception as fatal, a error message wich can be displayed to the user higher up leaves little room for client code trying to handle the exception. Even if the exception is fatal, things like diffirent locales (languages) makes formatting a message at the point of throw seem like a bad idea to me.
  2. Use tons of different exception classes derived from a base exception class. Just feels wrong introducing a new class/type for every single thing that can go wrong (open file, read file, write file, create thread, etc...). Catching all unhandled exceptions at the highest level using the base type loses type information required to display meaningfull error messages.
  3. Use one exception class derived from a base exception class for every component/library and give it a error code as argument to indicate the exact error. Catching with base type results in ambiguity. (Whose error code "3" did we catch? )...

Some pointers in the right direction would be welcome.

5
  • The simplest C++ exception mechanism I've seen is Symbian's take on it. Everything is an int, no exception classes or messages. A 'Leave' is synonymous with an exception: developer.nokia.com/Community/Wiki/Using_TRAP
    – James
    Commented Apr 26, 2012 at 22:26
  • 1
    (1) Exception strings are for programmers, not users. Don't use locales there. (2) How else could the compiler determine the right place to unwind to? (3) Obviously that's the problem with error codes. Don't do that. Commented Apr 26, 2012 at 22:27
  • @James: Simple is not always good. In that case I'd call it bad. Commented Apr 26, 2012 at 22:27
  • (2 again) if you're handling exceptions using the base types, you're probably doing it wrong. Commented Apr 26, 2012 at 22:28
  • @Mooing Duck: Why so? It works very well. I'd consider a class hierarchy for exceptions completely overkill
    – James
    Commented Apr 26, 2012 at 22:33

1 Answer 1

6

To address all three of these, I found that the best for me is to throw my own custom exception that's derived from std::runtime_error. Like this:

#include <exception>

class ChrisAException : public std::runtime_error
{
      /// constructor only which passes message to base class
      ChrisAException(std::string msg)
      : std::runtime_error(msg)
      {

      }
}

It allows accepting a string, which I always put in something like the following format (assuming x negative was not a valid input and meant something calling it was in error):

#include "ChrisAException.h"

void myfunction(int x)
{

    if(x < 0) 
    {
        throw ChrisAException("myfunction(): input was negative!");
    }

    // rest of your function
} 

For this one, keep in mind that the strings in these exceptions are more for the programmer than the end user. It's the programmer of the interface's job to display something meaningful in the locale when there's a failure. The strings in the exception can either be logged or seen at debug time (preferable!)

This way you can ultimately catch it with:

try
{
      // high level code ultimately calling myfunction

}
catch(ChrisAException &cae)
{
       // then log cae.what()
}
catch(std::runtime_error &e)
{
       // this will also catch ChrisAException's if the above block wasn't there
}
catch(...)
{
      // caught something unknown
}

I personally don't like deriving too many types of exceptions, or giving an error code. I let the string message do the reporting.

In general, I use C++ exceptions to mean "something went wrong with the program" and not to handle normal use cases. Thus, for me, a thrown exception during the algorithm execution either means "flag the user that something went wrong" or "don't tell the user" (depending on how critical that code was to what they were doing) but certainly log it and let the programmers know somehow.

I don't use C++ exceptions to handle cases that aren't essentially programming errors, e.g., some kind of incorrect logic or something being called wrong. For example, I wouldn't use C++ exceptions to handle normal program situations like an empty DVD not being in the drive for a DVD writing program. For that, I'd have explicit return code that allows the user to know whether an empty DVD was there (maybe with a dialog etc.)

Keep in mind that part of C++ exception handling is to unwind the stack up to a try-catch block. To me that means, abort what's going on in the program and clean up the stack. In the case of something like my DVD example, you shouldn't really want to unwind much of the stack. It wasn't catastrophic. You should simply let the user know and then let them try again.

But again, this is my preferred way of using C++ exceptions, based on experience and my reading. I'm open to opinions otherwise.

edit: Changed std::exception to std::runtime_error based on commenter advice.

9
  • How does the highest level catching code determine the kind of exception so it can try to handle it, or print a error message for the user? Compare the what string ? Commented Apr 26, 2012 at 21:47
  • Edited the post to explain this.
    – Chris A.
    Commented Apr 26, 2012 at 22:02
  • 3
    std::exception does not have a constructor that takes a string, only in MSVC's (non-conforming) implementation. Not terribly important, but still. For compilers that follow the standard in this regard, you can probably use std::runtime_error instead. Commented Apr 26, 2012 at 22:08
  • 1
    If you cannot "fix" the situation that caused the error, (which you cannot do because you don't even know the type of error), then the program is left in an unknown state. The only "correct" thing to do in that situation is to crash/reboot. There's articles about this all over the internet. Commented Apr 27, 2012 at 2:46
  • 2
    @MooingDuck: How is the program left in an unknown state if a catch (...) block is entered? Unwinding took place, destructors ran, transactions were rolled back, memory was freed. Something has gone wrong, but your invariants are protected.
    – Ben Voigt
    Commented Apr 30, 2012 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.