如何写一个完善的c++异常处理类
2016-05-22 17:30
447 查看
1: class DerivedException : public BaseException
2: {
3: public:
4: MY_DEFINE_EXCEPTION(DerivedException, BaseException);
5: };
1: MY_THROW(DerivedException)
1: catch (DerivedException& e)
2: {
3: cout<< e.what() << endl;
4: }
1: #ifndef EXCEPTION_TEST
2: #define EXCEPTION_TEST
3:
4: #include <exception>
5: #include <string>
6:
7: #define MY_THROW(ExClass, args...) \
8: do \
9: { \
10: ExClass e(args); \
11: e.Init(__FILE__, __PRETTY_FUNCTION__, __LINE__); \
12: throw e; \
13: } \
14: while (false)
15:
16: #define MY_DEFINE_EXCEPTION(ExClass, Base) \
17: ExClass(const std::string& msg = "") throw() \
18: : Base(msg) \
19: {} \
20: \
21: ~ExClass() throw() {} \
22: \
23: /* override */ std::string GetClassName() const \
24: { \
25: return #ExClass; \
26: }
27:
28: class ExceptionBase : public std::exception
29: {
30: public:
31: ExceptionBase(const std::string& msg = "") throw();
32:
33: virtual ~ExceptionBase() throw();
34:
35: void Init(const char* file, const char* func, int line);
36:
37: virtual std::string GetClassName() const;
38:
39: virtual std::string GetMessage() const;
40:
41: const char* what() const throw();
42:
43: const std::string& ToString() const;
44:
45: std::string GetStackTrace() const;
46:
47: protected:
48: std::string mMsg;
49: const char* mFile;
50: const char* mFunc;
51: int mLine;
52:
53: private:
54: enum { MAX_STACK_TRACE_SIZE = 50 };
55: void* mStackTrace[MAX_STACK_TRACE_SIZE];
56: size_t mStackTraceSize;
57: mutable std::string mWhat;
58: };
59:
60: class ExceptionDerived : public ExceptionBase
61: {
62: public:
63: MY_DEFINE_EXCEPTION(ExceptionDerived, ExceptionBase);
64: };
65:
66: #endif
1: #include <execinfo.h>
2: #include <stdlib.h>
3: #include <cxxabi.h>
4:
5: #include <iostream>
6: #include <sstream>
7:
8: #include "exception_test.h"
9:
10: using namespace std;
11:
12: ExceptionBase::ExceptionBase(const std::string& msg) throw()
13: : mMsg(msg),
14: mFile("<unknown file>"),
15: mFunc("<unknown func>"),
16: mLine(-1),
17: mStackTraceSize(0)
18: {}
19:
20: ExceptionBase::~ExceptionBase() throw()
21: {}
22:
23: void ExceptionBase::Init(const char* file, const char* func, int line)
24: {
25: mFile = file;
26: mFunc = func;
27: mLine = line;
28: mStackTraceSize = backtrace(mStackTrace, MAX_STACK_TRACE_SIZE);
29: }
30:
31: std::string ExceptionBase::GetClassName() const
32: {
33: return "ExceptionBase";
34: }
35:
36: const char* ExceptionBase::what() const throw()
37: {
38: return ToString().c_str();
39: }
40:
41: const std::string& ExceptionBase::ToString() const
42: {
43: if (mWhat.empty())
44: {
45: stringstream sstr("");
46: if (mLine > 0)
47: {
48: sstr << mFile << "(" << mLine << ")";
49: }
50: sstr << ": " << GetClassName();
51: if (!GetMessage().empty())
52: {
53: sstr << ": " << GetMessage();
54: }
55: sstr << "\nStack Trace:\n";
56: sstr << GetStackTrace();
57: mWhat = sstr.str();
58: }
59: return mWhat;
60: }
61:
62: std::string ExceptionBase::GetMessage() const
63: {
64: return mMsg;
65: }
66:
67: std::string ExceptionBase::GetStackTrace() const
68: {
69: if (mStackTraceSize == 0)
70: return "<No stack trace>\n";
71: char** strings = backtrace_symbols(mStackTrace, 10);
72: if (strings == NULL) // Since this is for debug only thus
73: // non-critical, don't throw an exception.
74: return "<Unknown error: backtrace_symbols returned NULL>\n";
75:
76: std::string result;
77: for (size_t i = 0; i < mStackTraceSize; ++i)
78: {
79: std::string mangledName = strings[i];
80: std::string::size_type begin = mangledName.find('(');
81: std::string::size_type end = mangledName.find('+', begin);
82: if (begin == std::string::npos || end == std::string::npos)
83: {
84: result += mangledName;
85: result += '\n';
86: continue;
87: }
88: ++begin;
89: int status;
90: char* s = abi::__cxa_demangle(mangledName.substr(begin, end-begin).c_str(),
91: NULL, 0, &status);
92: if (status != 0)
93: {
94: result += mangledName;
95: result += '\n';
96: continue;
97: }
98: std::string demangledName(s);
99: free(s);
100: // Ignore ExceptionBase::Init so the top frame is the
101: // user's frame where this exception is thrown.
102: //
103: // Can't just ignore frame#0 because the compiler might
104: // inline ExceptionBase::Init.
105: result += mangledName.substr(0, begin);
106: result += demangledName;
107: result += mangledName.substr(end);
108: result += '\n';
109: }
110: free(strings);
111: return result;
112: }
113:
114: /*
115: * test-main
116: */
117: int f2()
118: {
119: MY_THROW(ExceptionDerived, "f2 throw");
120: }
121: void f1()
122: {
123: try
124: {
125: f2();
126: }
127: catch (ExceptionDerived& e)
128: {
129: cout << e.what() << endl;
130: }
131: }
132: int main()
133: {
134: f1();
135: }