📄 ncbiexpt.hpp
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbiexpt.hpp,v $ * PRODUCTION Revision 1000.3 2004/06/01 19:08:02 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.54 * PRODUCTION * =========================================================================== */#ifndef NCBIEXPT__HPP#define NCBIEXPT__HPP/* $Id: ncbiexpt.hpp,v 1000.3 2004/06/01 19:08:02 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Denis Vakatov * * *//// @file ncbiexpt.hpp/// Defines NCBI C++ exception handling.////// Contains support for the NCBI C++ exception handling mechanisms and/// auxiliary ad hoc macros to "catch" certain types of errors, and macros for/// the C++ exception specification.#include <corelib/ncbidiag.hpp>#include <errno.h>#include <string.h>#include <string>#include <stdexcept>#include <typeinfo>/** @addtogroup Exception * * @{ */BEGIN_NCBI_SCOPE#if (_MSC_VER >= 1200)#undef NCBI_USE_THROW_SPEC#endif/// Define THROWS macros for C++ exception specification.////// Define use of C++ exception specification mechanism:/// "f(void) throw();" <== "f(void) THROWS_NONE;"/// "g(void) throw(e1,e2);" <== "f(void) THROWS((e1,e2));"#if defined(NCBI_USE_THROW_SPEC)# define THROWS_NONE throw()# define THROWS(x) throw x#else# define THROWS_NONE# define THROWS(x)#endif/// ABORT_ON_THROW controls if program should be aborted.#define ABORT_ON_THROW "ABORT_ON_THROW"/// Specify whether to call "abort()" inside the DoThrowTraceAbort().////// By default, this feature is not activated unless/// - environment variable $ABORT_ON_THROW is set (to any value), or/// - registry value of ABORT_ON_THROW, section DEBUG is set (to any value)extern void SetThrowTraceAbort(bool abort_on_throw_trace);/// "abort()" the program if set by SetThrowTraceAbort() or $ABORT_ON_THROW.NCBI_XNCBI_EXPORTextern void DoThrowTraceAbort(void);/// Print the specified debug message.NCBI_XNCBI_EXPORTextern void DoDbgPrint(const char* file, int line, const char* message);/// Print the specified debug message.NCBI_XNCBI_EXPORTextern void DoDbgPrint(const char* file, int line, const string& message);/// Print the specified debug messages.NCBI_XNCBI_EXPORTextern void DoDbgPrint(const char* file, int line, const char* msg1, const char* msg2);#if defined(_DEBUG)/// Templated function for printing debug message.////// Print debug message for the specified exception type.template<typename T>inlineconst T& DbgPrint(const char* file, int line, const T& e, const char* e_str){ DoDbgPrint(file, line, e_str, e.what()); return e;}/// Print debug message for "const char*" object.inlineconst char* DbgPrint(const char* file, int line, const char* e, const char* ){ DoDbgPrint(file, line, e); return e;}/// Print debug message for "char*" object.inlinechar* DbgPrint(const char* file, int line, char* e, const char* ){ DoDbgPrint(file, line, e); return e;}/// Print debug message for "std::string" object.inlineconst string& DbgPrint(const char* file, int line, const string& e, const char* ){ DoDbgPrint(file, line, e); return e;}/// Create diagnostic stream for printing specified message and "abort()" the/// program if set by SetThrowTraceAbort() or $ABORT_ON_THROW.////// @sa/// SetThrowTraceAbort(), DoThrowTraceAbort()template<typename T>inlineconst T& DbgPrintP(const char* file, int line, const T& e, const char* e_str){ CNcbiDiag(file, line, eDiag_Trace) << e_str << ": " << e; DoThrowTraceAbort(); return e;}/// Create diagnostic stream for printing specified message.////// Similar to DbgPrintP except that "abort()" not executed./// @sa/// DbgPrintP()template<typename T>inlineconst T& DbgPrintNP(const char* file, int line, const T& e, const char* e_str){ DoDbgPrint(file, line, e_str); return e;}/// Rethrow trace.////// Reason for do {...} while in macro definition is to permit a natural/// syntax usage when a user wants to write something like:////// if (expression)/// RETHROW_TRACE;/// else do_something_else;/// /// Example:/// - RETHROW_TRACE;# define RETHROW_TRACE do { \ _TRACE("EXCEPTION: re-throw"); \ NCBI_NS_NCBI::DoThrowTraceAbort(); \ throw; \} while(0)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Argument can be a simple string, or an exception object./// /// Example:/// - THROW0_TRACE("Throw just a string");/// - THROW0_TRACE(runtime_error("message"));# define THROW0_TRACE(exception_object) \ throw NCBI_NS_NCBI::DbgPrint(__FILE__, __LINE__, \ exception_object, #exception_object)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Argument can be any printable object; that is, any object with a defined/// output operator.////// Program may abort if so set by SetThrowTraceAbort() or $ABORT_ON_THROW.////// Example:/// - THROW0p_TRACE(123);/// - THROW0p_TRACE(complex(1,2));/// @sa/// THROW0np_TRACE# define THROW0p_TRACE(exception_object) \ throw NCBI_NS_NCBI::DbgPrintP(__FILE__, __LINE__, \ exception_object, #exception_object)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Argument can be any printable object; that is, any object with a defined/// output operator. ////// Similar to THROW0p_TRACE except that program is not "aborted" when/// exception is thrown, and argument type can be an aggregate type such as/// Vector<T> where T is a printable argument.////// Example:/// - THROW0np_TRACE(vector<char>());/// @sa/// THROW0p_TRACE# define THROW0np_TRACE(exception_object) \ throw NCBI_NS_NCBI::DbgPrintNP(__FILE__, __LINE__, \ exception_object, #exception_object)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Arguments can be any exception class with the specified initialization/// argument. The class argument need not be derived from std::exception as/// a new class object is constructed using the specified class name and /// initialization argument.////// Example:/// - THROW1_TRACE(runtime_error, "Something is weird...");# define THROW1_TRACE(exception_class, exception_arg) \ throw NCBI_NS_NCBI::DbgPrint(__FILE__, __LINE__, \ exception_class(exception_arg), #exception_class)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Arguments can be any exception class with a the specified initialization/// argument. The class argument need not be derived from std::exception as/// a new class object is constructed using the specified class name and /// initialization argument.////// Program may abort if so set by SetThrowTraceAbort() or $ABORT_ON_THROW.////// Example:/// - THROW1p_TRACE(int, 32);/// @sa/// THROW1np_TRACE# define THROW1p_TRACE(exception_class, exception_arg) \ throw NCBI_NS_NCBI::DbgPrintP(__FILE__, __LINE__, \ exception_class(exception_arg), #exception_class)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Arguments can be any exception class with a the specified initialization/// argument. The class argument need not be derived from std::exception as/// a new class object is constructed using the specified class name and /// initialization argument.////// Similar to THROW1p_TRACE except that program is not "aborted" when/// exception is thrown, and argument type can be an aggregate type such as/// Vector<T> where T is a printable argument.////// Example:/// - THROW1np_TRACE(CUserClass, "argument");# define THROW1np_TRACE(exception_class, exception_arg) \ throw NCBI_NS_NCBI::DbgPrintNP(__FILE__, __LINE__, \ exception_class(exception_arg), #exception_class)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Arguments can be any exception class with a the specified initialization/// arguments. The class argument need not be derived from std::exception as/// a new class object is constructed using the specified class name and /// initialization arguments.////// Similar to THROW1_TRACE except that the exception class can have multiple/// initialization arguments instead of just one.////// Example:/// - THROW_TRACE(bad_alloc, ());/// - THROW_TRACE(runtime_error, ("Something is weird..."));/// - THROW_TRACE(CParseException, ("Some parse error", 123));/// @sa/// THROW1_TRACE# define THROW_TRACE(exception_class, exception_args) \ throw NCBI_NS_NCBI::DbgPrint(__FILE__, __LINE__, \ exception_class exception_args, #exception_class)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Arguments can be any exception class with a the specified initialization/// arguments. The class argument need not be derived from std::exception as/// a new class object is constructed using the specified class name and /// initialization arguments.////// Program may abort if so set by SetThrowTraceAbort() or $ABORT_ON_THROW.////// Similar to THROW1p_TRACE except that the exception class can have multiple/// initialization arguments instead of just one.////// Example: /// - THROWp_TRACE(complex, (2, 3));/// @sa/// THROW1p_TRACE# define THROWp_TRACE(exception_class, exception_args) \ throw NCBI_NS_NCBI::DbgPrintP(__FILE__, __LINE__, \ exception_class exception_args, #exception_class)/// Throw trace.////// Combines diagnostic message trace and exception throwing. First the/// diagnostic message is printed, and then exception is thrown.////// Arguments can be any exception class with a the specified initialization/// argument. The class argument need not be derived from std::exception as/// a new class object is constructed using the specified class name and /// initialization argument.////// Argument type can be an aggregate type such as Vector<T> where T is a/// printable argument.////// Similar to THROWp_TRACE except that program is not "aborted" when/// exception is thrown.////// Example:/// - THROWnp_TRACE(CUserClass, (arg1, arg2));# define THROWnp_TRACE(exception_class, exception_args) \ throw NCBI_NS_NCBI::DbgPrintNP(__FILE__, __LINE__, \ exception_class exception_args, #exception_class)#else /* _DEBUG */// No trace/debug versions of these macros.# define RETHROW_TRACE \ throw# define THROW0_TRACE(exception_object) \ throw exception_object# define THROW0p_TRACE(exception_object) \ throw exception_object# define THROW0np_TRACE(exception_object) \ throw exception_object# define THROW1_TRACE(exception_class, exception_arg) \ throw exception_class(exception_arg)# define THROW1p_TRACE(exception_class, exception_arg) \ throw exception_class(exception_arg)# define THROW1np_TRACE(exception_class, exception_arg) \ throw exception_class(exception_arg)# define THROW_TRACE(exception_class, exception_args) \ throw exception_class exception_args# define THROWp_TRACE(exception_class, exception_args) \ throw exception_class exception_args# define THROWnp_TRACE(exception_class, exception_args) \ throw exception_class exception_args#endif /* else!_DEBUG *//// Standard handling of "exception"-derived exceptions.#define STD_CATCH(message) \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -