DIP33: A standard exception hierarchy

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 2 10:02:16 PDT 2013


On Tue, 02 Apr 2013 12:34:25 -0400, Jacob Carlborg <doob at me.com> wrote:

> 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.

Right, but what I see here is that the language uses one set of criteria  
to determine whether it should catch, but it's difficult to use that same  
criteria in order to process the exception.  It's not easy to switch on a  
class type, in fact it's downright ugly (maybe we need to come up with a  
way to do that in normal code too).

>> 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;
> }

Yes.  I won't forget to re-throw the exception.  Plus, it seems that you  
are saying "catch this, but if it's also that, then *really* catch it".  I  
think the catch is a one-shot deal, and should be the final disposition of  
the exception, you should rarely have to re-throw.  Re-throwing has it's  
own problems too, consider this possibility:

catch(ErrnoException ex) if(ex.errno == EBADF || ex.errno == EBADPARAM)
{
    // handle these specifically
}
catch(Exception ex)
{
    // handle all other exceptions
}

I think you would have to have nested try/catch statements to do that  
without something like this.

>> 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?
>

I mean it's a waste of code space and template bloat.  Not a waste to  
create the exception.

-Steve


More information about the Digitalmars-d mailing list