现在的位置: 首页 > 综合 > 正文

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

2013年10月01日 ⁄ 综合 ⁄ 共 3664字 ⁄ 字号 评论关闭
   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
}

抱歉!评论已关闭.