Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Installs your own termination function to be called by unexpected.

unexpected_function set_unexpected(
   unexpected_function unexp_func 


Pointer to a function that you write to replace the unexpected function.

Return Value

Returns a pointer to the previous termination function registered by _set_unexpected so that the previous function can be restored later. If no previous function has been set, the return value may be used to restore the default behavior; this value may be NULL.


The set_unexpected function installs unexp_func as the function called by unexpected. unexpected is not used in the current C++ exception-handling implementation. The unexpected_function type is defined in EH.H as a pointer to a user-defined unexpected function, unexp_func, that returns void. Your custom unexp_func function should not return to its caller.

typedef void ( *unexpected_function )( );

By default, unexpected calls terminate. You can change this default behavior by writing your own termination function and calling set_unexpected with the name of your function as its argument. unexpected calls the last function given as an argument to set_unexpected.

Unlike the custom termination function installed by a call to set_terminate, an exception can be thrown from within unexp_func.

In a multithreaded environment, unexpected functions are maintained separately for each thread. Each new thread needs to install its own unexpected function. Thus, each thread is in charge of its own unexpected handling.

In the current Microsoft implementation of C++ exception handling, unexpected calls terminate by default and is never called by the exception-handling run-time library. There is no particular advantage to calling unexpected rather than terminate.

There is a single set_unexpected handler for all dynamically linked DLLs or EXEs; even if you call set_unexpected your handler may be replaced by another or that you are replacing a handler set by another DLL or EXE.


Routine Required header Compatibility
set_unexpected <eh.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.

See Also

Exception Handling Routines | abort | set_terminate | terminate | unexpected | Run-Time Routines and .NET Framework Equivalents

© 2016 Microsoft