標準ヘッダー <exception> には、C++ 標準で示されている各種のクラスと例外用の関数が含まれています。このヘッダーは、標準モード (コンパイラのデフォルトモード、すなわち -compat=5 オプションを使用するモード) でコンパイルを行うときだけ使用されます。次は、<exception> ヘッダーファイルの宣言を抜粋したものです。
// 標準ヘッダー <exception>
namespace std {
class exception {
exception() throw();
exception(const exception&) throw();
exception& operator=(const exception&) throw();
virtual ~exception() throw();
virtual const char* what() const throw();
};
class bad_exception: public exception {...};
// 予期しない例外処理
typedef void (*unexpected_handler)();
unexpected_handler
set_unexpected(unexpected_handler) throw();
void unexpected();
// 終了処理
typedef void (*terminate_handler)();
terminate_handler set_terminate(terminate_handler) throw();
void terminate();
bool uncaught_exception() throw();
}
|
標準クラス exception は、構文要素や C++ 標準ライブラリから送出されるすべての例外のための基底クラスです。exception 型のオブジェクトは、例外を発生させることなく作成、複製、破棄することができます。仮想メンバー関数 what() は、例外についての情報を示す文字列を返します。
C++ release 4.2 で使用される例外との互換性を保つため、標準モードで使用する <exception.h> というヘッダーも用意されています。このヘッダーは、C++ 標準のコードに移行するためのもので、C++ 標準には含まれていない宣言を含んでいます。開発スケジュールに余裕があれば、<exception.h> の代わりに <exception> を使用し、コードを C++ 標準に従って書き換えてください。
// ヘッダー <exception.h>、移行用 #include <exception> #include <new> using std::exception; using std::bad_exception; using std::set_unexpected; using std::unexpected; using std::set_terminate; using std::terminate; typedef std::exception xmsg; typedef std::bad_exception xunexpected; typedef std::bad_alloc xalloc; |
互換モード (—compat[=4]) では、ヘッダー <exception> は使用できません。また、ヘッダー <exception.h> は、C++ release 4.2 で提供されているヘッダーと同じですので、ここでは取り上げません。