The Right Approach to Exceptions

Jonathan M Davis jmdavisProg at
Sun Feb 19 15:05:49 PST 2012

On Sunday, February 19, 2012 10:19:13 Andrei Alexandrescu wrote:
> On 2/19/12 2:49 AM, H. S. Teoh wrote:
> [snip]
> > To me, this is a giant hint that OOP is a good solution. You want a
> > class hierarchy rooted at Exception. And groups of related exceptions
> > probably should be rooted under their respective base classes under
> > Exception.
> > 
> > If you have a better way to solve this, I'm waiting to hear it.
> I don't. This thread is predicated on the assumption that the current
> approaches to exception handling are wanting, so it's time to look
> outside the box. It's also reasonable to assume that people involved in
> this thread do have an understanding of current mechanisms, so rehashes
> thereof are unnecessary.

Do you really believe that all of the current approaches are wanting? They may 
need some tweaking for us, but I don't think that they're necessarily all that 

Personally, I'd argue that we should look at how C# and Java have organized 
their exception hierarchies and then figure out which pieces of that make sense 
for Phobos and then do something similar. We don't have to copy what they did 
- we should tweak it according to our needs - but I really think that the 
basic exception hierarchy that they use tends to work very well. Where you 
tend to run into problems are where people do stupid things like catch 
Exception and ignore it. But you can't stop programmers from being stupid.

Already, we've managed to improve over Java (and probably C# - it's been a 
while since I used it though) in how we deal with Exception vs Error. So, 
we're a step ahead in that regard. But we don't have any kind of organized 
exception hierarchy right now. We simply have modules declaring their own 
whether it makes sense or not.uld do something similar. Perhaps it shouldn't

We should be looking at the exceptions that we currently have and figure out 
how they should be reorganized into a proper hierarchy, removing them in some 
cases and adding some in others. Java and C# have done at least a decent job 
with their exception hierarchies. We should do something similar. Maybe we 
won't have quite as many exception types, and maybe we organize some things 
quite differently, but I really think that we should be looking at what they 
did and and emulate it at least on some level. When and where we can come up 
with our own improvements, that's great. Let's use those. But let's not throw 
the baby out with the bath water.

I don't think that a complete redesign of how exceptions are designed is 
necessary. We just need to organize them better.

- Jonathan M Davis

More information about the Digitalmars-d mailing list