大佬教程收集整理的这篇文章主要介绍了c – 如何统一处理所有错误,包括内部C库错误,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如here所述,使用_set_se_translator捕获被零除错误.
但是它没有捕获,例如,C运行时库错误-1073740777(0xc0000417),这可能是由printf的格式字符串引起的,它们的百分号不应该出现. (这只是一个例子;当然我们应该检查这样的字符串).要处理这些,需要_set_invalid_parameter_handler.
here还列出了大约十个其他此类处理程序.
另外,这个将捕获未捕获的C异常:SetUnhandledExceptionFilter.因此,它可以与__set__ …函数一起使用. (MSVC 2008中关于using it的文章.)
我想捕获任何和所有错误,以便我可以处理它们(通过记录,抛出现代C标准异常并返回特定于应用程序的错误代码).是否有一个捕获一切的处理程序?
/////////////////////////////////////////////////////////////////////////// template<class K,class V> class MapInitializer { std::map<K,V> m; public: operator std::map<K,V>() const { return m; } MapInitializer& Add( const K& k,const V& v ) { m[ k ] = v; return *this; } }; /////////////////////////////////////////////////////////////////////////// struct StructuredException : std::exception { const char *const msg; StructuredException( const char* const msg_ ) : msg( msg_ ) {} virtual const char* what() const { return msg; } }; /////////////////////////////////////////////////////////////////////////// class ExceptionHandlerInstaller { public: ExceptionHandlerInstaller() : m_oldTerminateHandler( std::set_terminate( TerminateHandler ) ),m_oldUnexpectedHandler( std::set_unexpected( UnexpectedHandler ) ),m_oldSEHandler( _set_se_translator( SEHandler ) ) {} ~ExceptionHandlerInstaller() { std::set_terminate( m_oldTerminateHandler ); std::set_unexpected( m_oldUnexpectedHandler ); _set_se_translator( m_oldSEHandler ); } private: static void TerminateHandler() { TRACE( "\n\n**** terminate handler called! ****\n\n" ); } static void UnexpectedHandler() { TRACE( "\n\n**** unexpected exception handler called! ****\n\n" ); } static void SEHandler( const unsigned code,EXCEPTION_POINTERS* ) { SEMsgMap::const_iterator it = m_seMsgMap.find( code ); throw StructuredException( it != m_seMsgMap.end() ? it->second : "Structured exception translated to C++ exception." ); } const std::terminate_handler m_oldTerminateHandler; const std::unexpected_handler m_oldUnexpectedHandler; const _se_translator_function m_oldSEHandler; typedef std::map<unsigned,const char*> SEMsgMap; static const SEMsgMap m_seMsgMap; }; /////////////////////////////////////////////////////////////////////////// // message map for structured exceptions copied from the MS Help file /////////////////////////////////////////////////////////////////////////// const ExceptionHandlerInstaller::SEMsgMap ExceptionHandlerInstaller::m_seMsgMap = MapInitializer<ExceptionHandlerInstaller::SEMsgMap::key_type,ExceptionHandlerInstaller::SEMsgMap::mapped_type>() .Add( EXCEPTION_ACCESS_VIOLATION,"The thread attempts to read from or write to a virtual address for which it does not have access. This value is defined as STATUS_ACCESS_VIOLATION." ) .Add( EXCEPTION_ARRAY_BOUNDS_EXCEEDED,"The thread attempts to access an array element that is out of bounds,and the underlying hardware supports bounds checking. This value is defined as STATUS_ARRAY_BOUNDS_EXCEEDED." ) .Add( EXCEPTION_BREAKPOINT,"A breakpoint is encountered. This value is defined as STATUS_BREAKPOINT." ) .Add( EXCEPTION_DATATYPE_MISALIGNMENT,"The thread attempts to read or write data that is misaligned on hardware that does not provide alignment. For example,16-bit values must be aligned on 2-byte boundaries,32-bit values on 4-byte boundaries,and so on. This value is defined as STATUS_DATATYPE_MISALIGNMENT." ) .Add( EXCEPTION_FLT_DENORMAL_OPERAND,"One of the operands in a floating point operation is denormal. A denormal value is one that is too small to represent as a standard floating point value. This value is defined as STATUS_FLOAT_DENORMAL_OPERAND." ) .Add( EXCEPTION_FLT_DIVIDE_BY_ZERO,"The thread attempts to divide a floating point value by a floating point divisor of 0 (zero). This value is defined as STATUS_FLOAT_DIVIDE_BY_ZERO." ) .Add( EXCEPTION_FLT_INEXACT_RESULT,"The result of a floating point operation cAnnot be represented exactly as a @R_874_6221@l fraction. This value is defined as STATUS_FLOAT_INEXACT_RESULT." ) .Add( EXCEPTION_FLT_INVALID_OPERATION,"A floaTin point exception that is not included in this list. This value is defined as STATUS_FLOAT_INVALID_OPERATION." ) .Add( EXCEPTION_FLT_OVERFLOW,"The exponent of a floating point operation is greater than the magnitude allowed by the corresponding type. This value is defined as STATUS_FLOAT_OVERFLOW." ) .Add( EXCEPTION_FLT_STACK_checK,"The stack has overflowed or underflowed,because of a floating point operation. This value is defined as STATUS_FLOAT_STACK_checK." ) .Add( EXCEPTION_FLT_UNDERFLOW,"The exponent of a floating point operation is less than the magnitude allowed by the corresponding type. This value is defined as STATUS_FLOAT_UNDERFLOW." ) .Add( EXCEPTION_GUARD_PAGE,"The thread accessed memory allocated with the PAGE_GUARD modifier. This value is defined as STATUS_GUARD_PAGE_VIOLATION." ) .Add( EXCEPTION_ILLEGAL_instrUCTION,"The thread tries to execute an invalid instruction. This value is defined as STATUS_ILLEGAL_instrUCTION." ) .Add( EXCEPTION_IN_PAGE_ERROR,"The thread tries to access a page that is not present,and the system is unable to load the page. For example,this exception might occur if a network connection is lost while running a program over a network. This value is defined as STATUS_IN_PAGE_ERROR." ) .Add( EXCEPTION_INT_DIVIDE_BY_ZERO,"The thread attempts to divide an Integer value by an Integer divisor of 0 (zero). This value is defined as STATUS_IntegeR_DIVIDE_BY_ZERO." ) .Add( EXCEPTION_INT_OVERFLOW,"The result of an Integer operation causes a carry out of the most significant bit of the result. This value is defined as STATUS_IntegeR_OVERFLOW." ) .Add( EXCEPTION_INVALID_DISPOSITION,"An exception handler returns an invalid disposition to the exception dispatcher. ProgrAMMers using a high-level language such as C should never encounter this exception. This value is defined as STATUS_INVALID_DISPOSITION." ) .Add( EXCEPTION_INVALID_HANDLE,"The thread used a handle to a kernel object that was invalid (probably because it had been closed.) This value is defined as STATUS_INVALID_HANDLE." ) .Add( EXCEPTION_NONCONTinUABLE_EXCEPTION,"The thread attempts to conTinue execution after a non-conTinuable exception occurs. This value is defined as STATUS_NONCONTinUABLE_EXCEPTION." ) .Add( EXCEPTION_PRIV_instrUCTION,"The thread attempts to execute an instruction with an operation that is not allowed in the current computer mode. This value is defined as STATUS_PRIVILEGED_instrUCTION." ) .Add( EXCEPTION_SINGLE_STEP,"A trace trap or other single instruction mechanism signals that one instruction is executed. This value is defined as STATUS_SINGLE_STEP." ) .Add( EXCEPTION_STACK_OVERFLOW,"The thread uses up its stack. This value is defined as STATUS_STACK_OVERFLOW." );
然后在main或app init中,我这样做:
BOOL CMyApp::IniTinstance() { ExceptionHandlerInstaller ehi; // ... }
请注意,这会将结构化异常转换为常规异常,但会通过简单地打印错误消息来处理终止(例如,当nothrow()函数抛出异常时会调用它).您不太可能希望对所有不同类型的错误使用单个处理程序,这就是他们不提供错误的原因.
以上是大佬教程为你收集整理的c – 如何统一处理所有错误,包括内部C库错误全部内容,希望文章能够帮你解决c – 如何统一处理所有错误,包括内部C库错误所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。