DIP33: A standard exception hierarchy
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Mon Apr 1 17:46:46 PDT 2013
On Monday, 1 April 2013 at 23:52:52 UTC, Steven Schveighoffer
wrote:
> contrived example:
>
> class MyException : Exception {}
> class MySpecificException1 : MyException {}
> class MySpecificException2 : MyException {}
> class MySpecificException3 : MyException {}
>
> try
> {
> foo(); // can throw exception 1, 2, or 3 above
> }
> catch(MySpecificException1 ex)
> {
> // code block a
> }
> catch(MySpecificException2 ex)
> {
> // code block b
> }
>
> What if code block a and b are identical?
I was thinking about this too. And the most obvious answer in D
is not that great.
try {
foo(); // can throw 1, 2, or 3
}
catch ( Exception ex )
{
if ( cast( Exception1 ) ex !is null || cast( Exception2 ) ex
!is null )
{
// recovery code
}
else throw ex;
}
Ew. The first thing that comes to mind is separating the variable
from the condition, thus allowing multiple matches.
catch ex ( Exception1, Exception2 )
{
// recovery code
}
The necessary semantic caveat being that the type of 'ex' would
be the nearest common base type to the named exception types.
(The syntax is similar to some languages that have built-in error
types and the like.)
Combined with the previous proposal of being able to attach an
if-constraint to catch blocks, I suppose it could be rather
elaborate (powerful though?).
More information about the Digitalmars-d
mailing list