Nemiver  0.3
nmv-exception.h
Go to the documentation of this file.
1 /* -*- Mode: C++; indent-tabs-mode:nil; c-basic-offset: 4-*- */
2 
3 /*Copyright (c) 2005-2016 Dodji Seketeli
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS",
17  * WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE
19  * WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE
21  * AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23  * HOLDERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25  * CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
27  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  *
29  */
30 #ifndef __NMV_EXCEPTION_H__
31 #define __NMV_EXCEPTION_H__
32 
33 #include <cstdlib>
34 #include <stdexcept>
35 #include "nmv-ustring.h"
36 #include "nmv-log-stream-utils.h"
37 
38 namespace nemiver {
39 namespace common {
40 
41 class NEMIVER_EXCEPTION_API Exception: public std::runtime_error
42 {
43 public:
44  Exception (const char* a_reason);
45  Exception (const UString &a_reason);
46  Exception (const Exception &a_other);
47  Exception (const std::exception &);
48  Exception& operator= (const Exception &a_other);
49  virtual ~Exception () throw ();
50  const char* what () const throw ();
51 };//class Exception
52 
53 #define _THROW(EXPR) \
54 if (std::getenv ("nmv_abort_on_throw")) \
55  abort (); \
56 else \
57  throw (EXPR);
58 
59 #define _THROW_EMPTY \
60 if (std::getenv ("nmv_abort_on_throw")) \
61  abort (); \
62 else \
63  throw;
64 
65 #define THROW_IF_FAIL(a_cond) \
66 if (!(a_cond)) { \
67 LOG_EXCEPTION ("condition (" << #a_cond << ") failed; raising exception\n" );\
68 _THROW (nemiver::common::Exception \
69  (nemiver::common::UString ("Assertion failed: ") + #a_cond)) ;\
70 }
71 
72 #define THROW_IF_FAIL2(a_cond, a_reason) \
73 if (!(a_cond)) { \
74 LOG_EXCEPTION ("condition (" << #a_cond << ") failed; raising exception " << a_reason <<"\n");\
75 _THROW (nemiver::common::Exception (a_reason)) ;\
76 }
77 
78 #define THROW_IF_FAIL3(a_cond, type, a_reason) \
79 if (!(a_cond)) { \
80 LOG_EXCEPTION ("condition (" << #a_cond << ") failed; raising exception " << #type << \
81 << ": " << a_reason << "\n" ); _THROW (type (a_reason)) ;\
82 }
83 
84 #define ABORT_IF_FAIL(a_cond) \
85 if (!(a_cond)) { \
86 LOG_EXCEPTION ("condition (" << #a_cond << ") failed; aborting");\
87 }
88 
89 #define ABORT_IF_FAIL2(a_cond, a_reason) \
90 if (!(a_cond)) { \
91 LOG_EXCEPTION ("condition (" << #a_cond << ") failed; aborting because " << a_reason <<"\n"); abort();\
92 }
93 
94 #define ABORT(a_reason) \
95 do { \
96  LOG_EXCEPTION("aborting because: #a_reason"); abort(); \
97  } while (false)
98 
99 #define THROW(a_reason) \
100 LOG_EXCEPTION ("raised exception: "<< (nemiver::common::UString (a_reason)) << "\n"); \
101 _THROW (nemiver::common::Exception (nemiver::common::UString (a_reason))) ;
102 
103 #define THROW_EMPTY \
104 LOG_EXCEPTION ("raised empty exception " << endl); \
105 _THROW_EMPTY
106 
107 #define THROW_EXCEPTION(type, message) \
108 LOG_EXCEPTION ("raised " << #type << ": "<< message<< "\n"); \
109 _THROW (type (message));
110 
111 #define TRACE_EXCEPTION(exception) \
112 LOG_EXCEPTION ("catched exception: " << exception.what () << "\n")
113 
114 #define RETHROW_EXCEPTION(exception) \
115 LOG_EXCEPTION ("catched and rethrowing exception: " << exception.what() << "\n")
116 
117 #define RETURN_VAL_IF_FAIL(expression, value) \
118 if (!(expression)) { \
119 LOG_ERROR ("assertion " << #expression << " failed. Returning " << #value << "\n"); \
120 return value; \
121 }
122 
123 #define RETURN_IF_FAIL(expression) \
124 if (!(expression)) { \
125 LOG_ERROR ("assertion " << #expression << " failed. Returning.\n"); \
126 return; \
127 }
128 
129 #ifndef NEMIVER_TRY
130 #define NEMIVER_TRY try {
131 #endif
132 
133 #ifndef NEMIVER_CATCH_NOX
134 #define NEMIVER_CATCH_NOX \
135 } catch (Glib::Exception &e) { \
136  LOG_ERROR (e.what ()); \
137 } catch (std::exception &e) { \
138  LOG_ERROR (e.what ()); \
139 } catch (...) { \
140  LOG_ERROR ("An unknown error occured"); \
141 }
142 #endif
143 
144 #ifndef NEMIVER_CATCH_AND_RETURN_NOX
145 #define NEMIVER_CATCH_AND_RETURN_NOX(a_value) \
146 } catch (Glib::Exception &e) { \
147  LOG_ERROR (e.what ()); \
148  return a_value; \
149 } catch (std::exception &e) { \
150  LOG_ERROR (e.what ()); \
151  return a_value; \
152 } catch (...) { \
153  LOG_ERROR ("An unknown error occured"); \
154  return a_value; \
155 }
156 #endif
157 
158 }//end namespace common
159 }//end namespace nemiver
160 
161 #endif //__NMV_EXCEPTION_H__
nemiver
Definition: nmv-address.h:31
NEMIVER_EXCEPTION_API
#define NEMIVER_EXCEPTION_API
Definition: nmv-api-macros.h:51
nmv-ustring.h
nemiver::common::UString
Definition: nmv-ustring.h:45
nemiver::common::Exception
Definition: nmv-exception.h:41
nmv-log-stream-utils.h
common
Definition: nmv-proc-list-dialog.h:32