except.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2012 University of Pennsylvania
2 // Copyright (c) 2013-2016 Andreas Schuh
3 // All rights reserved.
4 //
5 // See COPYING file for license information or visit
6 // https://cmake-basis.github.io/download.html#license
7 
8 /**
9  * @file except.h
10  * @brief Basic exceptions and related helper macros.
11  */
12 
13 #pragma once
14 #ifndef _BASIS_EXCEPT_H
15 #define _BASIS_EXCEPT_H
16 
17 
18 #include <sstream> // used to compose exception messages
19 #include <stdexcept> // use standard STL exceptions where possible
20 #include <string> // used to store error messages
21 
22 #include "tclap/ArgException.h" // command-line parsing
23 
24 
25 namespace basis {
26 
27 
28 // ===========================================================================
29 // convenience macros
30 // ===========================================================================
31 
32 /**
33  * @brief Throw exception with given message.
34  *
35  * Example:
36  * @code
37  * void func(int i) {
38  * if (i < 0) BASIS_THROW(std::invalid_argument, "Argument i (= " << i << ") must be positive");
39  * }
40  * @endcode
41  *
42  * @param [in] type The type of the exception. Note that this exception
43  * type has to implement a constructor with one std::string
44  * as argument, the exception message.
45  * @param [in] msg The exception message. The given argument is streamed
46  * into a std::ostringstream.
47  */
48 #define BASIS_THROW(type, msg) \
49  { \
50  ::std::ostringstream oss; \
51  oss << msg; \
52  throw type(oss.str().c_str()); \
53  }
54 
55 // ===========================================================================
56 // exceptions
57 // ===========================================================================
58 
59 /// @brief Exception thrown by command-line parsing library.
61 
62 /// @brief Exception thrown by command-line parsing library to indicate that
63 /// program should exit with the given exit code.
65 
66 /// @brief Exception thrown on command-line argument parsing error.
68 
69 /// @brief Exception thrown on command-line parsing error.
71 
72 /// @brief Exception thrown when command-line specification is wrong.
73 typedef TCLAP::SpecificationException CmdLineException;
74 
75 
76 } // namespace basis
77 
78 
79 #endif // _BASIS_EXCEPT_H
Definition: basis.h:34
TCLAP::ArgParseException ArgParseException
Exception thrown on command-line argument parsing error.
Definition: except.h:67
TCLAP::SpecificationException CmdLineException
Exception thrown when command-line specification is wrong.
Definition: except.h:73
TCLAP::ArgException ArgException
Exception thrown by command-line parsing library.
Definition: except.h:60
TCLAP::ExitException ExitException
Exception thrown by command-line parsing library to indicate that program should exit with the given ...
Definition: except.h:64
TCLAP::CmdLineParseException CmdLineParseException
Exception thrown on command-line parsing error.
Definition: except.h:70