你能通过转换运算符的类型捕获异常吗?
我不知道如何用简短的主题行很好地表达这个问题,所以让我尝试更长的解释。假设我有这些异常类:
class ExceptionTypeA : public std::runtime_error
{
// stuff
};
class ExceptionTypeB : public std::runtime_error
{
// stuff
operator ExceptionTypeA() const; // conversion operator to ExceptionTypeA
};
我可以这样做,并让它触发 catch 块吗?
try
{
throw ExceptionTypeB();
}
catch (ExceptionTypeA& a)
{
// will this be triggered?
}
我猜它不会,这是不幸的,但我想我会问,因为我在网上或SO上找不到任何有关它的信息。是的,我意识到我可以在编译器中运行该程序,看看会发生什么,但这不会告诉我标准对这种行为的说明,只是我的编译器实现了什么(我不相信它)。
I don't know how to phrase the question very well in a short subject line, so let me try a longer explanation. Suppose I have these exception classes:
class ExceptionTypeA : public std::runtime_error
{
// stuff
};
class ExceptionTypeB : public std::runtime_error
{
// stuff
operator ExceptionTypeA() const; // conversion operator to ExceptionTypeA
};
Can I then do this, and have it trigger the catch block?
try
{
throw ExceptionTypeB();
}
catch (ExceptionTypeA& a)
{
// will this be triggered?
}
I'm going to guess that it will not, which is unfortunate, but I thought I'd ask, since I couldn't find any info on it on the net or on SO. And yes, I realize I could just run the program in my compiler and see what happens, but that wouldn't tell me what the standard says about this behavior, just what my compiler implements (and I don't trust it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。标准语
15.3/3
:您想要的场景与这些都不匹配。
cv
表示“常量和/或易失性组合”You cannot. Standardese at
15.3/3
:Your desired scenario matches none of these.
cv
means "const and/or volatile combination"