DIP33: A standard exception hierarchy

Jacob Carlborg doob at me.com
Tue Apr 2 09:34:25 PDT 2013


On 2013-04-02 15:44, Steven Schveighoffer wrote:

> Yes, this could help.  But it's still not great.  One must still store a
> "type identifier" in the ex, or have to deal with casting to figure out
> what type ex is.

You would have the same problem if you used a common function for 
handling multiple exceptions.

> It also promotes creating a new type for every single catchable situation.
>
> Consider that we could create one exception type that contains an
> 'errno' member, and if we have the ability to run extra checks for
> catching you could do:
>
> catch(ErrnoException ex) if (ex.errno == EBADF || ex.errno == EBADPARAM)
> {
>     ...
> }

Is that so much better than:

catch (ErrnoException ex)
{
     if (ex.errno == EBADF || ex.errno == EBADPARAM)
     { /*handle exception */ }
     else
         throw ex;
}

> But if we must do it with types, we need:
>
> class ErrnoException(uint e) : Exception
> {
>     enum errno = e;
> }
>
> or worse, to make things easier to deal with we have:
>
> class ErrnoException : Exception
> {
>     int errno;
>     this(int errno) { this.errno = errno; }
> }
>
> class ErrnoExceptionT(uint e) : ErrnoException
> {
>     this() { super(e); }
> }
>
> which would be easier to deal with, but damn what a waste!  Either way,
> every time you catch another errno exception, we are talking about
> instantiating another type.

Does that matter? It still need to create a new instance for every 
exception thrown. Or are you planning on changing the "errno" field and 
rethrow the exception?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list