共用方式為


<exception> 函式

current_exception

取得目前例外狀況的智慧型指標。

exception_ptr current_exception();

傳回值

指向目前例外狀況的 exception_ptr 物件。

備註

在 catch 區塊中呼叫 current_exception 函式。 如果例外狀況正在進行中,而且 catch 區塊可以攔截例外狀況,則 current_exception 函式會傳回參考例外狀況的 exception_ptr 物件。 否則,函式會傳回 Null exception_ptr 物件。

不論 catch 陳述式是否指定例外狀況宣告陳述式,current_exception 函式都會擷取執行中的例外狀況。

若不重新擲回例外狀況,則會在 catch 區塊結尾呼叫目前例外狀況的解構函式。 不過,即使在解構函式中呼叫 current_exception 函式,該函式仍會傳回參考目前例外狀況的 exception_ptr 物件。

current_exception 函式的後續呼叫會傳回參考目前例外狀況不同複本的 exception_ptr 物件。 因此,物件比較結果會是不相等,因為兩者參考不同的複本 (即使複本的二進位值相同也一樣)。

make_exception_ptr

建立持有例外狀況複本的 exception_ptr 物件。

template <class E>
    exception_ptr make_exception_ptr(E Except);

參數

除了
具有待複製例外狀況的類別。 雖然任何類別物件都可以是 make_exception_ptr 函式的引數,但一般會指定 exception 類別物件作為其引數。

傳回值

指向 Except 目前例外 狀況複本的 exception_ptr 物件。

備註

呼叫 make_exception_ptr 函式相當於擲回 C++ 例外狀況、在 catch 區塊中攔截例外狀況,然後呼叫 current_exception 函式以傳回參考例外狀況的 exception_ptr 物件。 Microsoft 實作make_exception_ptr 函式比在擲回例外狀況之後攔截來得更有效率。

一般來說,應用程式通常不需要使用 make_exception_ptr 函式,所以我們不建議使用此功能。

rethrow_exception

擲回做為參數傳遞的例外狀況。

void rethrow_exception(exception_ptr P);

參數

P
要重新擲回的已攔截例外狀況。 如果 P 是 null exception_ptr ,函式會 擲回 std::bad_exception

備註

將攔截到的例外狀況儲存在 exception_ptr 物件之後,主執行緒即可處理物件。 在主執行緒中呼叫 rethrow_exception 函式,並使用 exception_ptr 物件做為其引數。 rethrow_exception 函式會從 exception_ptr 物件擷取例外狀況,然後在主執行緒的內容中擲回該例外狀況。

get_terminate

取得目前的 terminate_handler 函式。

terminate_handler get_terminate();

set_terminate

建立新 terminate_handler,在程式終止時呼叫。

terminate_handler set_terminate(terminate_handler fnew) throw();

參數

fnew
要在終止時呼叫的函式。

傳回值

先前用來在終止時呼叫的函式位址。

備註

函式會建立新的 terminate_handler 做為函式 * fnew 。 因此, fnew 不得為 Null 指標。 此函數會傳回上一個終止處理常式的位址。

範例

// exception_set_terminate.cpp
// compile with: /EHsc
#include <exception>
#include <iostream>

using namespace std;

void termfunction()
{
    cout << "My terminate function called." << endl;
    abort();
}

int main()
{
    terminate_handler oldHandler = set_terminate(termfunction);

    // Throwing an unhandled exception would also terminate the program
    // or we could explicitly call terminate();

    //throw bad_alloc();
    terminate();
}

get_unexpected

取得目前的 unexpected_handler 函式。

unexpected_handler get_unexpected();

rethrow_if_nested

template <class E>
    void rethrow_if_nested(const E& e);

備註

如果不是多型類別類型,或者 nested_exception 無法存取或模棱兩可,則沒有任何作用。 否則,會執行動態轉換。

set_unexpected

建立新的 unexpected_handler,當未預期的例外狀況發生時擲回。

unexpected_handler set_unexpected(unexpected_handler fnew) throw();

參數

fnew
當未預期的例外狀況發生時要呼叫的函式。

傳回值

上一個 unexpected_handler 的位址。

備註

fnew 不得為 Null 指標。

C++ 標準要求在函式擲回例外狀況時,必須呼叫 unexpected。 目前的實作不支援此項目。 下列範例會直接呼叫 unexpected,這會引發呼叫 unexpected_handler

範例

// exception_set_unexpected.cpp
// compile with: /EHsc
#include <exception>
#include <iostream>

using namespace std;

void uefunction()
{
    cout << "My unhandled exception function called." << endl;
    terminate(); // this is what unexpected() calls by default
}

int main()
{
    unexpected_handler oldHandler = set_unexpected(uefunction);

    unexpected(); // library function to force calling the
                  // current unexpected handler
}

terminate

呼叫終止處理常式。

void terminate();

備註

函式會呼叫終止處理常式,其為 void 類型的函式。 如果 terminate 直接由程式呼叫,則 terminate 處理常式是最近由呼叫 set_terminate 所設定的處理常式。 如果在 terminate 評估擲回運算式期間因其他數個原因而呼叫 ,則終止處理常式是在評估 throw 運算式之後立即生效的處理常式。

終止處理常式可能無法傳回至其呼叫端。 在程式啟動時,終止處理常式是呼叫 abort 的函式。

範例

如需 terminate 的用法範例,請參閱 set_unexpected

throw_with_nested

template <class T> [[noreturn]]
    void throw_with_nested(T&& t);

備註

擲回具有巢狀例外狀況的例外狀況。

uncaught_exception

只有當系統正在處理擲回的例外狀況時,才傳回 true

bool uncaught_exception();

傳回值

在完成 throw 運算式評估之後,以及完成相符處理常式中例外狀況宣告的初始化之前,或呼叫 unexpected 作為 throw 運算式的結果之前,系統會傳回 true。 特別之處在於:如果呼叫 uncaught_exception 的解構函式在例外狀況回溯期間受到叫用,則會傳回 trueuncaught_exception 僅支援 Windows CE 5.00 或更新版本的裝置,包括 Windows Mobile 2005 平台。

範例

// exception_uncaught_exception.cpp
// compile with: /EHsc
#include <exception>
#include <iostream>
#include <string>

class Test
{
public:
   Test( std::string msg ) : m_msg( msg )
   {
      std::cout << "In Test::Test(\"" << m_msg << "\")" << std::endl;
   }
   ~Test( )
   {
      std::cout << "In Test::~Test(\"" << m_msg << "\")" << std::endl
         << "        std::uncaught_exception( ) = "
         << std::uncaught_exception( )
         << std::endl;
   }
private:
    std::string m_msg;
};

// uncaught_exception will be true in the destructor
// for the object created inside the try block because
// the destructor is being called as part of the unwind.

int main( void )
   {
      Test t1( "outside try block" );
      try
      {
         Test t2( "inside try block" );
         throw 1;
      }
      catch (...) {
   }
}
In Test::Test("outside try block")
In Test::Test("inside try block")
In Test::~Test("inside try block")
        std::uncaught_exception( ) = 1
In Test::~Test("outside try block")
        std::uncaught_exception( ) = 0

非預期

呼叫未預期的處理常式。

void unexpected();

備註

C++ 標準要求在函式擲回例外狀況時,必須呼叫 unexpected。 目前的實作不支援此項目。 範例會直接呼叫 unexpected,這會引發呼叫非預期的處理常式。

函式會呼叫未預期的處理常式,其為 void 類型的函式。 如果程式直接呼叫 unexpected,未預期的處理常式即為 set_unexpected 呼叫最近設定的項目。

未預期的處理常式可能無法傳回至其呼叫端。 它可能會透過下列方式來終止程式執行:

  • 擲回例外狀況規格中提列的物件,或者,如果程式直接呼叫未預期的處理常式,則會擲回任何類型的物件。

  • 擲回 bad_exception 類型的物件。

  • 呼叫 terminate abortexit

在程式啟動時,未預期的處理常式會呼叫一個函數,其會呼叫 terminate

範例

如需 unexpected 的用法範例,請參閱 set_unexpected