Thread overview
types of exceptions
Nov 23, 2005
Ameer Armaly
Nov 23, 2005
MWolf
Nov 26, 2005
Chris Sauls
November 23, 2005
Hi.
Maybe I'm missing something, but why does almost every module in phobos have its own type of exception, which is just a wrapper for the base class?

-- 



Ameer
---
Visit my blog at
http://ameerarmaly.blogspot.com
---
Life is either tragedy or comedy.
 Usually it's your choice. You can whine or you can laugh.
--Animorphs

November 23, 2005
In article <dm2o11$1t5e$1@digitaldaemon.com>, Ameer Armaly says...
>
>This is a multi-part message in MIME format.
>
>------=_NextPart_000_000E_01C5F04D.994A0440
>Content-Type: text/plain;
>	charset="iso-8859-1"
>Content-Transfer-Encoding: quoted-printable
>
>Hi.
>Maybe I'm missing something, but why does almost every module in phobos =
>have its own type of exception, which is just a wrapper for the base =
>class?
>
>--=20
>
>
>
>Ameer
>---
>Visit my blog at
>http://ameerarmaly.blogspot.com
>---
>Life is either tragedy or comedy.
> Usually it's your choice. You can whine or you can laugh.
>--Animorphs
>------=_NextPart_000_000E_01C5F04D.994A0440
>Content-Type: text/html;
>	charset="iso-8859-1"
>Content-Transfer-Encoding: quoted-printable
>
><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
><HTML><HEAD>
><META http-equiv=3DContent-Type content=3D"text/html; =
>charset=3Diso-8859-1">
><META content=3D"MSHTML 6.00.2900.2627" name=3DGENERATOR>
><STYLE></STYLE>
></HEAD>
><BODY bgColor=3D#ffffff>
><DIV><FONT face=3DArial size=3D2>Hi.</FONT></DIV>
><DIV><FONT face=3DArial size=3D2>Maybe I'm missing something, but why =
>does almost=20
>every module in phobos have its own type of exception, which is just a =
>wrapper=20
>for the base class?</FONT></DIV><FONT face=3DArial size=3D2>
><DIV><BR>-- <BR></DIV>
><DIV>&nbsp;</DIV>
><DIV>&nbsp;</DIV>
><DIV>Ameer<BR>---<BR>Visit my blog at<BR><A=20
>href=3D"http://ameerarmaly.blogspot.com">http://ameerarmaly.blogspot.com<=
>/A><BR>---<BR>Life=20
>is either tragedy or comedy.<BR>&nbsp;Usually it's your choice. You can =
>whine or=20
>you can laugh.<BR>--Animorphs</FONT></DIV></BODY></HTML>
>
>------=_NextPart_000_000E_01C5F04D.994A0440--
>

So you can catch specific exceptions?


November 26, 2005
Ameer Armaly wrote:
> Hi.
> Maybe I'm missing something, but why does almost every module in phobos have its own type of exception, which is just a wrapper for the base class?
> 
> Ameer

I'll give you an explanation by giving you an (heavily watered down) example from one of my own projects.  This particular program runs one of a number of functions based on user input ("command lines" essentially).  These functions, in turn, might throw one of "InvalidArgsException" or "InvalidStateException."  Both of these are just "empty children" as I like to say, or in other words:

# class InvalidStateException : Exception { /*empty*/ }

(( On a side note, maybe it'd be nifty to be able to just use a semicolon in these cases?  Something like 'class Foo : Bar;' ...or not, its personal taste, I suppose. ))

Now then, these functions get called with code along these lines:

# try {
#   Commands.exec(input);
# }
# catch (UnknownCommandException ucx) {
#   // report exception, check for similar commands to suggest
# }
# catch (InvalidArgsException iax) {
#   // report exception, print usage text
# }
# catch (InvalidStateException isx) {
#   // report exception, print current state (makes sense in context)
#   // make command recommendation based on state (aka, what was most likely meant)
# }

So, while the new exceptions don't directly add any functionality, they allow me to take real advantage of the try{}catch{} syntax.  The alternative is to use a single Exception child with a "Type" enum.  Something like:

# enum MyXType {
#   UnknownCommand, InvalidArgs, InvalidState
# }
#
# class MyException : Exception {
#   this (char[] msg, MyXType t) {
#     super(msg);
#     p_type = t;
#   }
#
#   MyXType type () { return p_type; }
#
#   private MyXType p_type;
# }

But then my try{}catch{} turns into something like:

# try {
#   Commands.exec(input);
# }
# catch (MyException mx) {
#   switch (mx.type()) {
#     case MyXType.UnknownCommand:
#       ... break;
#
#     case MyXType.InvalidArgs:
#       ... break;
#
#     case MyXType.InvalidState:
#       ... break;
#
#     default:
#       /* eek?! */
#   }
# }

I like the first way better, myself.  :)

-- Chris Sauls