The Right Approach to Exceptions

Jose Armando Garcia jsancio at
Sat Feb 18 17:26:35 PST 2012

On Sat, Feb 18, 2012 at 11:19 PM, Jonathan M Davis <jmdavisProg at> wrote:
> On Saturday, February 18, 2012 22:54:37 Jose Armando Garcia wrote:
>> You basically agree that we need error codes inside the exception to
>> handle database errors correctly. What about POSIX error codes?
>> Basically in some cases you are solving the problem using inheritance
>> in other cases you are solving the problem by switching on a variable.
> There is a world of difference between having a specific exception type have an
> error code from an external source (e.g. a database or POSIX) and making _all_
> exceptions function via error codes.
> And personally, I'd argue that in many cases, the correct thing to do would be
> to have a base class exception type which has the error code in it (e.g.
> FileException) and then have derived exceptions where the one you use depends
> on what the error code actually was. Then additional information pertaining to
> that specific error code could be put in the derived exception, and you can
> catch the exceptions by type rather than having to resort to switching on the
> error type in the exception.
> That way, you can actually take advantage of the exception hierarchy for
> controlling how you handle the exception, and you have additional information
> pertaining to what went wrong. And if you want access to the original error
> code, you have it. You can even just catch the base exception type and use a
> switch if you want to. But you don't have to.

I think that is a good compromise. The one thing we need to solve is
that for example Oracle's error code may collide with POSIX error
code. This is why I suggested using namespace but there may be other


> - Jonathan M Davis

More information about the Digitalmars-d mailing list