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* tail
);
86 void construct(const char* header
, const char* extra
,
87 const char* function
);
89 static std::string
format_extra(const char* condStr
, const char* argDesc
);
91 static char* s_header
;
95 IllegalArgumentException(const char* condStr
, const char* argDesc
,
96 const char* function
, const char* tail
) :
98 construct(s_header
, format_extra(condStr
, argDesc
).c_str(), function
, tail
);
101 IllegalArgumentException(const char* condStr
, const char* argDesc
,
102 const char* function
) :
104 construct(s_header
, format_extra(condStr
, argDesc
).c_str(), function
);
108 * This is a convenience function for building usages that are variadic.
110 * Having IllegalArgumentException itself be variadic is problematic for
111 * making sure calls to IllegalArgumentException clean up memory.
113 static std::string
formatVariadic();
114 static std::string
formatVariadic(const char* format
, ...);
115 };/* class IllegalArgumentException */
117 inline std::ostream
& operator<<(std::ostream
& os
, const Exception
& e
) throw() CVC4_PUBLIC
;
118 inline std::ostream
& operator<<(std::ostream
& os
, const Exception
& e
) throw() {
123 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
,
124 const char* tail
) CVC4_PUBLIC
;
125 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
,
127 if(__builtin_expect( ( !cond
), false )) { \
128 throw ::CVC4::IllegalArgumentException("", "", ""); \
131 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
)
133 template <class T
> inline void CheckArgument(bool cond
, const T
& arg
) {
134 if(__builtin_expect( ( !cond
), false )) { \
135 throw ::CVC4::IllegalArgumentException("", "", ""); \
140 }/* CVC4 namespace */
142 #endif /* __CVC4__EXCEPTION_H */