BASIS  r3148
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, 2012 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 _BASIS_EXCEPT_H
00013 #define _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 "tclap/ArgException.h" // command-line parsing
00021 
00022 
00023 namespace basis {
00024 
00025 
00026 // ===========================================================================
00027 // convenience macros
00028 // ===========================================================================
00029 
00030 /**
00031  * @brief Throw exception with given message.
00032  *
00033  * Example:
00034  * @code
00035  * void func(int i) {
00036  *     if (i < 0) BASIS_THROW(std::invalid_argument, "Argument i (= " << i << ") must be positive");
00037  * }
00038  * @endcode
00039  *
00040  * @param [in] type The type of the exception. Note that this exception
00041  *                  type has to implement a constructor with one std::string
00042  *                  as argument, the exception message.
00043  * @param [in] msg  The exception message. The given argument is streamed
00044  *                  into a std::ostringstream.
00045  */
00046 #define BASIS_THROW(type, msg) \
00047     { \
00048        ::std::ostringstream oss; \
00049        oss << msg; \
00050        throw type(oss.str().c_str()); \
00051     }
00052 
00053 // ===========================================================================
00054 // exceptions
00055 // ===========================================================================
00056 
00057 /// @brief Exception thrown by command-line parsing library.
00058 typedef TCLAP::ArgException ArgException;
00059 
00060 /// @brief Exception thrown by command-line parsing library to indicate that
00061 ///        program should exit with the given exit code.
00062 typedef TCLAP::ExitException ExitException;
00063 
00064 /// @brief Exception thrown on command-line argument parsing error.
00065 typedef TCLAP::ArgParseException ArgParseException;
00066 
00067 /// @brief Exception thrown on command-line parsing error.
00068 typedef TCLAP::CmdLineParseException CmdLineParseException;
00069 
00070 /// @brief Exception thrown when command-line specification is wrong.
00071 typedef TCLAP::SpecificationException CmdLineException;
00072 
00073 
00074 } // namespace basis
00075 
00076 
00077 #endif // _BASIS_EXCEPT_H