异常捕获 [英] exception catch

查看:75
本文介绍了异常捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有以下例外课程,


班级E1

{

};


class E2

{

public:E2(const E1&)

{

printf(自动类型转换\ n);

}

};


int main()

{

试试

{

扔E1( );

}

catch(E2 obj)

{

printf(" first handler\\\
) ;);

}

catch(E1 obj1)

{

printf(" second handler\ n");

}

}


在上述情况下,E1对象未转换为E2并且

由第二个处理程序处理,这表明自动类型

转换不会发生异常,我想知道这是否是

指定在标准中,有什么理由吗?


提前致谢!!!

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?

Thanks in advance!!!

推荐答案

Rahul写道:
Rahul wrote:

大家好,


我有以下例外课程,


班级E1

{

};


级E2

{

public:E2(const E1&)

{

printf(" automatic类型转换\ n");

}

};


int main()

{

试试

{

抛出E1();

}

catch(E2 obj)
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)



不要通过值捕获异常,使用const引用。


-

Ian Collins。

Don''t catch exceptions by value, use a const reference.

--
Ian Collins.


Rahul写道:
Rahul wrote:

大家好,

我有以下例外课程,


班级E1

{

};


class E2

{

public:E2(const E1&)

{
printf(自动类型转换\ n);

}

};


int main()

{

试试

{

抛出E1();

}

catch(E2 obj)

{

printf(" first handler\\\
);

}

catch(E1 obj1)

{

printf(" second handler\\\
);

}

}


在上述情况下,E1对象未转换为E2并且是

由第二个处理程序处理,这表明自动类型

转换不会发生异常,我想知道这是否是标准中指定的
并且是否有任何原因为了那个原因?
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?



它在[15.3 / 3]中指定。


至于一个原因,我只能猜测。但是,我猜想

的问题是,当你抛出时,你并不一定能控制这个问题。即,库组件可能会精确抛出因为它无法在本地处理该条件并希望客户端代码提供一个

catch处理程序。如果你允许在这个设置中进行用户定义的转换,

可能会有惊喜的匹配。

最好


启-Uwe Bux

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


文章

< 9a ******************* *************** @ i29g2000prf。 googlegroups.com>,

Rahul< sa ***** @ yahoo.co.inwrote:
In article
<9a**********************************@i29g2000prf. googlegroups.com>,
Rahul <sa*****@yahoo.co.inwrote:

大家好,


我有以下异常类,


class E1

{

};


class E2

{

public:E2(const E1&)

{

printf(自动类型转换\ n);

}

};


int main()

{

试试

{

抛出E1();

}

catch(E2 obj)

{

printf(" first handler\\\
);

}

catch(E1 obj1)

{

printf(" second handler\ n);

}

}


在上述情况下,E1对象未转换为E2并且处理了

由第二个处理程序,这表明自动类型

转换不会发生异常,我很想念如果这是

在标准中指定并且有什么理由吗?
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?



根据15.3,这是正确的。在我看来,这是一个理想的

行为,因为异常处理程序用于处理特定的异常,

不仅仅是任何可以方便转换的异常。想象一下,当你有一个更合适的异常处理程序更高的时候,

会出现混乱

调用树,以及下面的其他一些异常处理程序会抢占它

由于自动类型转换。


请注意,如果异常处理程序是其参数类型的派生

类,它将处理异常。


-dr

That is correct, according to 15.3. In my opinion, this is a desired
behavior, as exception handlers are meant to handle specific exceptions,
not simply any exception that can be conveniently converted. Imagine the
confusion when you have a more-appropriate exception handler higher up
the call tree, and some other exception handler down below preempts it
due to automatic type conversion.

Note that exception handlers will handle an exception if it is a derived
class of its parameter type.

-dr


这篇关于异常捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆