请注意以下代码:
struct exception_in_copy_constructor
{
exception_in_copy_constructor() = default;
~exception_in_copy_constructor() = default;
exception_in_copy_constructor(const exception_in_copy_constructor& other)
{
throw std::exception(":-(");
}
};
Run Code Online (Sandbox Code Playgroud)
现在,很明显当我抛出这个对象时,会抛出一个std :: exception.
那么让我们看看下面的代码:
try
{
exception_in_copy_constructor ecc;
throw ecc;
}
catch(exception_in_copy_constructor& ecc)
{
printf("IN exception_in_copy_constructor&\r\n");
}
catch(std::exception& ex)
{
printf("IN std::exception&\r\n");
}
Run Code Online (Sandbox Code Playgroud)
我希望之前的ecc会被抛出,std :: exception会被抛出,因此,ecc永远不会被抛出.上面的代码确认通过打印"IN std :: exception&"
但请注意这段代码:
try
{
exception_in_copy_constructor ecc;
throw ecc;
}
catch(exception_in_copy_constructor& ecc)
{
printf("IN exception_in_copy_constructor&\r\n");
}
Run Code Online (Sandbox Code Playgroud)
我希望没有任何东西会被抓住,但令我惊讶的是, catch(exception_in_copy_constructor& ecc)
抓住了异常(?!?!)
谁能解释一下发生了什么?
我正在使用VS2015
谢谢!
编辑:用户写道他们无法重现,所以我正在编写整个代码,因为它在我的计算机上.另外,我使用的是VS2015,debug,x64(它还在发行版中重现).
这是代码:
int main()
{
struct exception_in_copy_constructor
{
exception_in_copy_constructor() = default;
~exception_in_copy_constructor() = default;
exception_in_copy_constructor(const exception_in_copy_constructor& other)
{
throw std::exception(":-(");
}
};
try
{
exception_in_copy_constructor ecc;
throw ecc;
}
catch(exception_in_copy_constructor&)
{
printf("We will never get here!\r\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
82 次 |
最近记录: |