예외가 발생하는지 확인하는 방법이 있습니까?
-
02-07-2019 - |
문제
파괴자에서는 현재 예외가 처리 중인지 확인할 수있는 방법이 있습니까?
해결책
std :: uncaught_exception ()을 사용할 수는 있지만 생각하는 일을하지 않을 수도 있습니다. gotw#47 자세한 내용은.
다른 팁
Luc가 말했듯이 std :: uncaught_exception ()을 사용할 수 있습니다. 하지만 왜 알고 싶어합니까? 어쨌든 소멸자는 예외를 제외하지 않아야합니다!
당신은 사용할 수 있습니다 테스트 라이브러리 부스트. 작은 예를 보려면 여기를 참조하십시오.
struct my_exception1
{
explicit my_exception1( int res_code ) : m_res_code( res_code ) {}
int m_res_code;
};
struct my_exception2
{
explicit my_exception2( int res_code ) : m_res_code( res_code ) {}
int m_res_code;
};
class dangerous_call {
public:
dangerous_call( int argc ) : m_argc( argc ) {}
int operator()()
{
if( m_argc < 2 )
throw my_exception1( 23 );
if( m_argc > 3 )
throw my_exception2( 45 );
else if( m_argc > 2 )
throw "too many args";
return 1;
}
private:
int m_argc;
};
void translate_my_exception1( my_exception1 const& ex )
{
std::cout << "Caught my_exception1(" << ex.m_res_code << ")"<< std::endl;
}
void translate_my_exception2( my_exception2 const& ex )
{
std::cout << "Caught my_exception2(" << ex.m_res_code << ")"<< std::endl;
}
int
cpp_main( int argc , char *[] )
{
::boost::execution_monitor ex_mon;
ex_mon.register_exception_translator<my_exception1>(
&translate_my_exception1);
ex_mon.register_exception_translator<my_exception2>(
&translate_my_exception2);
try{
// ex_mon.detect_memory_leak( true);
ex_mon.execute( ::boost::unit_test::callback0<int>(
dangerous_call( argc ) ) );
}
catch ( boost::execution_exception const& ex ) {
std::cout << "Caught exception: " << ex.what() << std::endl;
}
return 0;
}
문서를 파야합니다. 소프트웨어를 테스트하는 것은 매우 강력한 라이브러리입니다! 어쨌든 부스트의 도움으로 기능 테스트의 어느 곳에서나 모든 종류의 예외를 잡을 수 있습니다!
제휴하지 않습니다 StackOverflow