The Right Approach to Exceptions

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 18 15:20:14 PST 2012


On Saturday, February 18, 2012 17:13:16 Andrei Alexandrescu wrote:
> On 2/18/12 4:26 PM, Jonathan M Davis wrote (abridged):
> GetOptException
> FlagArgumentMissingException
> InvalidFlagArgumentException
> UnknownFlagException
> FileException
> FileNotFoundException
> NotFileException
> NotDirException
> AccessDeniedException
> 
> I died inside a little.

If you actually want to _handle_ exceptions, how else do you expect to do it? 
Simply put a code of some kind on the exceptions and then have switch 
statement to handle them? If you want to go beyond simply printing out an 
error message when an exception occurs, having a solid exception hierarchy is 
the way to go. Otherwise, all that your program is has is the knowledge that 
something went wrong and a message that it could print out to the user which 
might inform them as to want went wrong, but it has _nothing_ which will help 
the program itself handle the problem appropriately.

Honestly, I think that on the whole, this is something that Java got right. 
They might use exceptions for more than they should (e.g. if the odds of a 
function failing are very high, don't use an exception, have it return whether 
it succeeded or not - Java pretty much never does that). But on the whole, I 
think that they have the correct approach.

- Jonathan M Davis


More information about the Digitalmars-d mailing list