C++ プロジェクトの汎用エラー ハンドラを作成しています。ロギングの一部として、例外クラスの名前を含めたいと考えています。dynamic_cast とロジック ツリーを使用せずに、std::exception インスタンスから特定のエラー クラスの名前を一般的に取得する方法があることを願っています。
例:
exception_handler.h
#pragma once
#include <exception>
#include <string>
class ExceptionHandler
{
public:
static std::string get_exception_type_name(std::exception ex)
{
return ((std::string)typeid(ex).name()).substr(11);
}
};
main.cpp
#include <iostream>
#include "exception_handler.h"
int _tmain(int argc, _TCHAR* argv[])
{
std::string any = "any";
std::out_of_range ex("Out of range exception");
std::cout << ExceptionHandler::get_exception_type_name(ex) << std::endl;
std::cout << "Press any key to close this window..." << std::endl;
std::cin >> any;
}
実行すると「例外」が出力されます。「out_of_range」または関数にフィードする他の種類の派生例外を言いたいです。
前もって感謝します。