1 #ifndef SimTK_SimTKCOMMON_EXCEPTION_H_
2 #define SimTK_SimTKCOMMON_EXCEPTION_H_
29 #pragma warning(disable:4996)
45 class Base :
public std::exception {
47 explicit Base(
const char* fn=
"<UNKNOWN>",
int ln=0)
48 : fileName(fn), lineNo(ln) { }
58 msg =
"SimTK Exception thrown at " + where() +
":\n " + msgin;
66 static std::string shortenFileName(
const std::string& fn)
67 { std::string::size_type pos = fn.find_last_of(
"/\\");
68 if (pos+1>=fn.size()) pos=0;
69 return std::string(fn,(
int)(pos+1),(
int)(fn.size()-(pos+1)));
72 std::string where()
const {
74 sprintf(buf,
"%d",lineNo);
75 return shortenFileName(fileName) +
":" + std::string(buf);
87 Assert(
const char* fn,
int ln,
const char* assertion,
88 const char* fmt ...) :
Base(fn,ln)
93 vsprintf(buf, fmt, args);
95 setMessage(
"Internal bug detected: " + std::string(buf)
96 +
"\n (Assertion '" + std::string(assertion) +
"' failed).\n"
97 " Please file an Issue at https://github.com/simbody/simbody/issues.\n"
98 " Include the above information and anything else needed to reproduce the problem.");
115 const char* whereChecked,
116 const char* fmt ...) :
Base(fn,ln)
121 vsprintf(buf, fmt, args);
123 setMessage(
"Error detected by Simbody method "
124 + std::string(whereChecked) +
": "
126 +
"\n (Required condition '" + std::string(assertion) +
"' was not met.)\n");
142 const char* className,
const char* methodName,
143 const char* fmt ...) :
Base(fn,ln)
148 vsprintf(buf, fmt, args);
150 + std::string(className) +
"::" + std::string(methodName) +
"(): "
152 +
"\n (Required condition '" + std::string(assertion) +
"' was not met.)");
162 long long lb,
long long index,
long long ub,
const char* where)
167 sprintf(buf,
"Index out of range in %s: expected %lld <= %s < %lld but %s=%lld.",
168 where,lb,indexName,ub,indexName,index);
177 unsigned long long sz,
unsigned long long maxsz,
const char* where)
182 sprintf(buf,
"Size out of range in %s: expected 0 <= %s <= %llu but %s=%llu.",
183 where,szName,maxsz,szName,sz);
192 unsigned long long sz,
const char* where)
197 sprintf(buf,
"Size argument was negative in %s: expected 0 <= %s but %s=%llu.",
198 where,szName,szName,sz);
207 double lowerBound,
double value,
double upperBound,
213 sprintf(buf,
"Value out of range in %s: expected %g <= %s <= %g but %s=%g.",
214 where,lowerBound,valueName,upperBound,valueName,value);
223 double value,
const char* where)
228 sprintf(buf,
"Expected non-negative value for %s in %s but got %g.",
229 valueName,where,value);
241 +
"is not yet implemented. Please post to the Simbody forum"
242 " to find a workaround or request implementation.");
250 std::string baseClass, std::string methodName)
254 " dummy implementation of method " + methodName
255 +
"() was invoked because a derived class did not provide an implementation.");
264 setMessage(
"Attempt to assign a Value<"+src+
"> to a Value<"+dest+
">");
273 setMessage(
"Operation '" + op +
"' allowed only for owners, not views");
282 setMessage(
"Operation '" + op +
"' allowed only for views, not owners");
291 setMessage(
"Operation '" + op +
"' not allowed on non-const readonly view");
299 Cant(
const char* fn,
int ln,
const std::string& s) :
Base(fn,ln)
309 #define SimTK_THROW(exc) \
310 throw exc(__FILE__, __LINE__)
311 #define SimTK_THROW1(exc,a1) \
312 throw exc(__FILE__, __LINE__,a1)
313 #define SimTK_THROW2(exc,a1,a2) \
314 throw exc(__FILE__, __LINE__,a1,a2)
315 #define SimTK_THROW3(exc,a1,a2,a3) \
316 throw exc(__FILE__, __LINE__,a1,a2,a3)
317 #define SimTK_THROW4(exc,a1,a2,a3,a4) \
318 throw exc(__FILE__, __LINE__,a1,a2,a3,a4)
319 #define SimTK_THROW5(exc,a1,a2,a3,a4,a5) \
320 throw exc(__FILE__, __LINE__,a1,a2,a3,a4,a5)
321 #define SimTK_THROW6(exc,a1,a2,a3,a4,a5,a6) \
322 throw exc(__FILE__, __LINE__,a1,a2,a3,a4,a5,a6)
323 #define SimTK_THROW7(exc,a1,a2,a3,a4,a5,a6,a7) \
324 throw exc(__FILE__, __LINE__,a1,a2,a3,a4,a5,a6,a7)
325 #define SimTK_THROW8(exc,a1,a2,a3,a4,a5,a6,a7,a8) \
326 throw exc(__FILE__, __LINE__,a1,a2,a3,a4,a5,a6,a7,a8)
327 #define SimTK_THROW9(exc,a1,a2,a3,a4,a5,a6,a7,a8,a9) \
328 throw exc(__FILE__, __LINE__,a1,a2,a3,a4,a5,a6,a7,a8,a9)
329 #define SimTK_THROW10(exc,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) \
330 throw exc(__FILE__, __LINE__,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10)
332 #endif // SimTK_SimTKCOMMON_EXCEPTION_H_