1 /********************* */
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief CVC4's exception base class and some associated utilities
14 ** CVC4's exception base class and some associated utilities.
17 #include "cvc4_public.h"
19 #ifndef __CVC4__EXCEPTION_H
20 #define __CVC4__EXCEPTION_H
32 class CVC4_PUBLIC Exception
: public std::exception
{
38 Exception() throw() : d_msg("Unknown exception") {}
39 Exception(const std::string
& msg
) throw() : d_msg(msg
) {}
40 Exception(const char* msg
) throw() : d_msg(msg
) {}
43 virtual ~Exception() throw() {}
45 // NON-VIRTUAL METHOD for setting and printing the error message
46 void setMessage(const std::string
& msg
) throw() { d_msg
= msg
; }
47 std::string
getMessage() const throw() { return d_msg
; }
49 // overridden from base class std::exception
50 virtual const char* what() const throw() { return d_msg
.c_str(); }
53 * Get this exception as a string. Note that
54 * cout << ex.toString();
55 * is subtly different from
57 * which is equivalent to
59 * That is because with the latter two, the output language (and
60 * other preferences) for exprs on the stream is respected. In
61 * toString(), there is no stream, so the parameters are default
62 * and you'll get exprs and types printed using the AST language.
64 std::string
toString() const throw() {
71 * Printing: feel free to redefine toStream(). When overridden in
72 * a derived class, it's recommended that this method print the
73 * type of exception before the actual message.
75 virtual void toStream(std::ostream
& os
) const throw() { os
<< d_msg
; }
77 };/* class Exception */
79 class CVC4_PUBLIC IllegalArgumentException
: public Exception
{
81 IllegalArgumentException() : Exception() {}
83 void construct(const char* header
, const char* extra
,
84 const char* function
, const char* fmt
, ...) {
87 construct(header
, extra
, function
, fmt
, args
);
91 void construct(const char* header
, const char* extra
,
92 const char* function
, const char* fmt
, va_list args
);
94 void construct(const char* header
, const char* extra
,
95 const char* function
);
98 IllegalArgumentException(const char* condStr
, const char* argDesc
,
99 const char* function
, const char* fmt
, ...) :
103 construct("Illegal argument detected",
104 ( std::string("`") + argDesc
+ "' is a bad argument"
105 + (*condStr
== '\0' ? std::string() :
106 ( std::string("; expected ") +
107 condStr
+ " to hold" )) ).c_str(),
108 function
, fmt
, args
);
112 IllegalArgumentException(const char* condStr
, const char* argDesc
,
113 const char* function
) :
115 construct("Illegal argument detected",
116 ( std::string("`") + argDesc
+ "' is a bad argument"
117 + (*condStr
== '\0' ? std::string() :
118 ( std::string("; expected ") +
119 condStr
+ " to hold" )) ).c_str(),
122 };/* class IllegalArgumentException */
124 inline std::ostream
& operator<<(std::ostream
& os
, const Exception
& e
) throw() CVC4_PUBLIC
;
125 inline std::ostream
& operator<<(std::ostream
& os
, const Exception
& e
) throw() {
130 }/* CVC4 namespace */
132 #if (defined(__BUILDING_CVC4LIB) || defined(__BUILDING_CVC4LIB_UNIT_TEST)) && !defined(__BUILDING_STATISTICS_FOR_EXPORT)
133 # include "util/cvc4_assert.h"
134 #endif /* (__BUILDING_CVC4LIB || __BUILDING_CVC4LIB_UNIT_TEST) && !__BUILDING_STATISTICS_FOR_EXPORT */
138 #ifndef CheckArgument
139 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
, const char* fmt
, ...) CVC4_PUBLIC
;
140 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
, const char* fmt
, ...) {
141 if(__builtin_expect( ( !cond
), false )) { \
142 throw ::CVC4::IllegalArgumentException("", "", ""); \
145 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
) CVC4_PUBLIC
;
146 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
) {
147 if(__builtin_expect( ( !cond
), false )) { \
148 throw ::CVC4::IllegalArgumentException("", "", ""); \
151 #endif /* CheckArgument */
153 #ifndef DebugCheckArgument
154 template <class T
> inline void DebugCheckArgument(bool cond
, const T
& arg
, const char* fmt
, ...) CVC4_PUBLIC
;
155 template <class T
> inline void DebugCheckArgument(bool cond
, const T
& arg
, const char* fmt
, ...) {
156 if(__builtin_expect( ( !cond
), false )) { \
157 throw ::CVC4::IllegalArgumentException("", "", ""); \
160 template <class T
> inline void DebugCheckArgument(bool cond
, const T
& arg
) CVC4_PUBLIC
;
161 template <class T
> inline void DebugCheckArgument(bool cond
, const T
& arg
) {
162 if(__builtin_expect( ( !cond
), false )) { \
163 throw ::CVC4::IllegalArgumentException("", "", ""); \
166 #endif /* DebugCheckArgument */
168 }/* CVC4 namespace */
170 #endif /* __CVC4__EXCEPTION_H */