Re: exception catch
Rahul wrote:
> Hi Everyone,
>
> I have the following exception class,
>
> class E1
> {
> };
>
> class E2
> {
> public: E2(const E1&)
> {
> printf("automatic type conversion\n");
> }
> };
>
> int main()
> {
> try
> {
> throw E1();
> }
> catch(E2 obj)
> {
> printf("first handler\n");
> }
> catch(E1 obj1)
> {
> printf("second handler\n");
> }
> }
>
> IN the above case, the E1 object is not converted into E2 and is
> handled by the second handler, this suggests that automatic type
> conversion doesn't happen with exceptions, i was wondering if this is
> specified in standards and is there any reason for that?
It is specified in [15.3/3].
As for a reason, I can only guess. However, I would conjecture that the
problem is that when you throw, you do not necessarily have control over
the catch. I.e., a library component might throw precisely because it
cannot handle the condition locally and wants the client code to supply a
catch handler. If you allowed for user-defined conversions in this setting,
there could be surprise catch-matches.
Best
Kai-Uwe Bux
|