2008 July 15th Tuesday (七月 十五日 火曜日)

本文深入探讨了C++中异常处理机制的关键概念,包括未捕获异常和未预期异常的处理方式,以及如何通过自定义terminate和unexpected函数来改变程序默认的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   After an exception has been thrown, it has two opportunities to cause problems. First, if it is thrown in a function
having an exception specification, it has to match one of the types in the specification list. If it doesn't, the unmatched
exception is branded an unexpected exception, and, by default, it causes the program to abort. If the exception passes
this first hurdle (or avoids it because the function lacks an exception specification), it then has to be caught. If it
isn't, which can happen if there is no containing try block or no matching catch block, the exception is branded an uncaught
exception, and, by default, it causes the program to abort. However, you can alter a program's response to unexpected and
uncaught exceptions. Let's see how, beginning with uncaught exceptions.

  An uncaught exception doesn't initiate an immediate abort. Instead, the program first calls a function called terminate().
By default, terminate() calls the abort() function. You can modify the behavior of terminate() by registering a function that
terminate() should call instead of abort(). To do this, call the set_terminate() function. Both set_terminate() and terminate()
are declared in the exception header file:

typedef void (*terminate_handler)();
terminate_handler set_terminate(terminate_handler f) throw();
void terminate();

  The set_terminate() function takes, as its argument, the name of a function (that is, its address) having no arguments and
the void return type. It returns the address of the previously registered function. If you call the set_terminate() function
more than once, terminate() calls the function set by the most recent call to set_terminate().

  The behavior is much like that for uncaught exceptions. If there is an unexpected exception, the program calls the unexpected()
function. (You didn't expect the unexpected() function? No one expects the unexpected() function!) This function, in turn, calls
terminate(), which, by default, calls abort(). Just as there is a set_terminate() function that modifies the behavior of terminate(),
there is a set_unexpected() function that modifies the behavior of unexpected(); these new functions also are declared in the exception
header file:

typedef void (*unexpected_handler)();
unexpected_handler set_unexpected(unexpected_handler f) throw();
void unexpected();

  However, the behavior of the function you supply set_unexpected() is more regulated than that of a function for set_terminate().
In particular, the unexpected_handler function has the following choices:

  * It can end the program by calling terminate() (the default behavior), abort(), or exit().
  * It can throw an exception.

  The result of throwing an exception (the second choice above) depends upon the exception thrown by the replacement unexpected_handler
function and the original exception specification for the function that threw the unexpected type:

  * If the newly thrown exception matches the original exception specification, then the program proceeds normally from there; that is, it
  will look for a catch block matching the newly thrown exception. Basically, this approach replaces an exception of an unexpected type to
  an exception of an expected type.

  * If the newly thrown exception does not match the original exception specification and if the exception specification does not include
  the std::bad_exception type, the program calls terminate(). The bad_exception type derives from the exception type and is declared in
  the exception header file.

  * If the newly thrown exception does not match the original exception specification and if the original exception specification does include
  the std::bad_exception type, the unmatched exception is replaced with an exception of the std::bad_exception type.

void unexpected_handler(){
  // calling terminate(), abort() or exit().

or

  //throw an exception
  unexpected_exception -> newly_exception;

  throw newly_exception;
}

/*if (newly_exception "matches" one of the original exception specification){
   normally look for a catch block ...;
}
else{  // not match
   if (std::bad_exception is not in the original exception specification)
     terminate();
   else
     unmatched_exception -> std::bad_exception;
     // the unmatched_exception is the newly_exception
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值