BASIS  version 1.2.3 (revision 2104)
except.h
Go to the documentation of this file.
00001 /**
00002  * @file  except.h
00003  * @brief Basic exceptions and related helper macros.
00004  *
00005  * Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br />
00006  * See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00007  *
00008  * Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00009  */
00010 
00011 #pragma once
00012 #ifndef _SBIA_BASIS_EXCEPT_H
00013 #define _SBIA_BASIS_EXCEPT_H
00014 
00015 
00016 #include <sstream>   // used to compose exception messages
00017 #include <stdexcept> // use standard STL exceptions where possible
00018 #include <string>    // used to store error messages
00019 
00020 #include <sbia/tclap/ArgException.h> // command-line parsing
00021 
00022 
00023 namespace sbia
00024 {
00025 
00026 namespace basis
00027 {
00028 
00029 
00030 // ===========================================================================
00031 // convenience macros
00032 // ===========================================================================
00033 
00034 /**
00035  * @brief Throw exception with given message.
00036  *
00037  * Example:
00038  * @code
00039  * void func(int i) {
00040  *     if (i < 0) BASIS_THROW(std::invalid_argument, "Argument i (= " << i << ") must be positive");
00041  * }
00042  * @endcode
00043  *
00044  * @param [in] type The type of the exception. Note that this exception
00045  *                  type has to implement a constructor with one std::string
00046  *                  as argument, the exception message.
00047  * @param [in] msg  The exception message. The given argument is streamed
00048  *                  into a std::ostringstream.
00049  */
00050 #define BASIS_THROW(type, msg) \
00051     { \
00052        ::std::ostringstream oss; \
00053        oss << msg; \
00054        throw type(oss.str().c_str()); \
00055     }
00056 
00057 // ===========================================================================
00058 // exceptions
00059 // ===========================================================================
00060 
00061 /// @brief Exception thrown by command-line parsing library.
00062 typedef TCLAP::ArgException ArgException;
00063 
00064 /// @brief Exception thrown by command-line parsing library to indicate that
00065 ///        program should exit with the given exit code.
00066 typedef TCLAP::ExitException ExitException;
00067 
00068 /// @brief Exception thrown on command-line argument parsing error.
00069 typedef TCLAP::ArgParseException ArgParseException;
00070 
00071 /// @brief Exception thrown on command-line parsing error.
00072 typedef TCLAP::CmdLineParseException CmdLineParseException;
00073 
00074 /// @brief Exception thrown when command-line specification is wrong.
00075 typedef TCLAP::SpecificationException CmdLineException;
00076 
00077 /**
00078  * @class SubprocessException
00079  * @brief Exception type thrown by execute_process().
00080  */
00081 class SubprocessException : public ::std::exception
00082 {
00083 public:
00084     SubprocessException(const ::std::string& msg) : msg_(msg) {}
00085     ~SubprocessException() throw () {}
00086 
00087 private:
00088     ::std::string msg_; ///< Error message.
00089 }; // class SubprocessException
00090 
00091 
00092 } // namespace basis
00093 
00094 } // namespace sbia
00095 
00096 
00097 #endif // _SBIA_BASIS_EXCEPT_H