如果抛出会发生什么;语句在catch块外部执行? [英] What happens if a throw; statement is executed outside of catch block?
问题描述
在C ++ throw;
当在catch块内执行时,会重新抛出当前捕获的异常。
在此回答中,提出了异常分派器的概念当使用复杂的异常处理时,通常会减少代码重复:
try {
CodeThatMightThrow
} catch(...){
ExceptionHandler();
}
无效ExceptionHandler()
{
try {
throw;
} catch(FileException * e){
//处理一些复杂的逻辑
delete e;
} catch(GenericException * e){
//处理其他复杂逻辑
delete e;
}
}
抛出指针或值不会
如果ExceptionHandler()不是从catch块调用,会发生什么?
<我尝试这个代码与VC7:
int main(int,char **)
{
try {
throw;
} catch(...){
MessageBox(0,,,0);
}
return 0;
}
首先它使调试器指示第一次机会异常,未处理的异常。如果我在调试器外运行这个代码,程序崩溃的方式与调用abort()时一样。
这种情况下的预期行为是什么?
从标准中,15.1 / 8
如果目前没有处理异常,则执行不带操作数调用
std :: terminate
()的 throw-expression 。
In C++ throw;
when executed inside a catch block rethrows the currently caught exception outside the block.
In this answer an idea of exception dispatcher is brought up as a solution to reducing code duplication when using complex exception handling often:
try {
CodeThatMightThrow();
} catch(...) {
ExceptionHandler();
}
void ExceptionHandler()
{
try {
throw;
} catch( FileException* e ) {
//do handling with some complex logic
delete e;
} catch( GenericException* e ) {
//do handling with other complex logic
delete e;
}
}
Throwing a pointer or a value doesn't make any difference so it's out of the question.
What happens if ExceptionHandler() is called not from a catch block?
I tried this code with VC7:
int main( int, char** )
{
try {
throw;
} catch( ... ) {
MessageBox( 0, "", "", 0 );
}
return 0;
}
First it causes the debugger to indicate a first-chance exception, then immediately an unhandled exception. If I run this code outside the debugger the program crashes the same way as if abort() has been called.
What is the expected behaviour for such situations?
From the Standard, 15.1/8
If no exception is presently being handled, executing a throw-expression with no operand calls
std::terminate
().
这篇关于如果抛出会发生什么;语句在catch块外部执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!