Topic: Catching built-in types


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/21
Raw View
In article 2006951501080001@intrigue.intrigue.com, rac@intrigue.com (Robert Coie) writes:
>I couldn't find a discussion in the draft standard relating to exception
>handler matching for built-in types, specifically integral types and
>enums.  Does integral promotion apply when searching for an appropriate
>handler?  Is there any way to catch an anonymous enum?

Any conversion may be performed, even a user-defined conversion. When
searching for a handler, the first one found is used for which the
thrown value can be converted to the type of the handler's parameter.
That is why you must write catch clauses in order of most-specific to
least-specific.

You can catch any numeric or enum type with any catch clause having a parameter
of any integer or floating type.

If the catch clause has an enum type, it can catch only enums of the same
type, of course, since there is no implicit conversion to enum.

Example:
    try { ... }
    catch( Enum1 e ) { ... } // catches enum Enum1 only
    catch( Enum2 e ) { ... } // catches enum Enum2 only
    catch( int i ) { ... }   // catches any numeric type:
                             // bool, char, short, int, long, enum, float, double
---
Steve Clamage, stephen.clamage@eng.sun.com







Author: rac@intrigue.com (Robert Coie)
Date: 1995/06/20
Raw View
I couldn't find a discussion in the draft standard relating to exception
handler matching for built-in types, specifically integral types and
enums.  Does integral promotion apply when searching for an appropriate
handler?  Is there any way to catch an anonymous enum?

Robert Coie                              rac@intrigue.com
Implementor, Intrigue Corporation     AppleLink: INTRIGUE





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/21
Raw View
In article r2d@engnews2.Eng.Sun.COM, clamage@Eng.Sun.COM (Steve Clamage) writes:
>In article 2006951501080001@intrigue.intrigue.com, rac@intrigue.com (Robert Coie) writes:
>>I couldn't find a discussion in the draft standard relating to exception
>>handler matching for built-in types, specifically integral types and
>>enums.  Does integral promotion apply when searching for an appropriate
>>handler?  Is there any way to catch an anonymous enum?
>
>Any conversion may be performed, even a user-defined conversion.

Oh, my. I was just plain wrong.

Here is the relevent part of the draft standard (15.3):

"A handler with type T, const T, T&, or const T& is a match for a
throw   expression with an object of type E if
 [1] T and E are the same type, or
 [2] T is a public base class of E, or
 [3] T is a pointer type and E is a pointer type that can be
converted to T by a standard pointer conversion (4.10) not involving
conversions to pointers to private or protected base classes."

So in particular, no conversion of enums will occur. You can catch
an enum value only with a catch clause using the same enum type, or
with one using an ellipsis (which catches any exception).

---
Steve Clamage, stephen.clamage@eng.sun.com