E-MailRelay

gexception.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gexception.h
19///
20
21#ifndef G_EXCEPTION_H
22#define G_EXCEPTION_H
23
24#include "gdef.h"
25#include <string>
26#include <iostream>
27#include <stdexcept>
28
29 namespace G
30{
31 class Exception ;
32}
33
34//| \class G::Exception
35/// A general-purpose exception class derived from std::exception and containing
36/// an error message. Provides constructors that simplify the assembly of multi-part
37/// error messages.
38///
39/// Usage:
40/// \code
41/// throw G::Exception( "initialisation error" , "no such file" , path ) ;
42/// \endcode
43///
44 class G::Exception : public std::runtime_error
45{
46 public:
47 explicit Exception( const char * what ) ;
48 ///< Constructor.
49
50 explicit Exception( const std::string & what ) ;
51 ///< Constructor.
52
53 Exception( const char * what , const std::string & more ) ;
54 ///< Constructor.
55
56 Exception( const std::string & what , const std::string & more ) ;
57 ///< Constructor.
58
59 Exception( const char * what , const std::string & more1 , const std::string & more2 ) ;
60 ///< Constructor.
61
62 Exception( const std::string & what , const std::string & more1 , const std::string & more2 ) ;
63 ///< Constructor.
64
65 Exception( const char * what , const std::string & more1 , const std::string & more2 , const std::string & more3 ) ;
66 ///< Constructor.
67
68 Exception( const std::string & what , const std::string & more1 , const std::string & more2 , const std::string & more3 ) ;
69 ///< Constructor.
70} ;
71
72#define G_EXCEPTION_CLASS( class_name , description ) class class_name : public G::Exception { public: class_name() : G::Exception((description)) {} explicit class_name(const char *more) : G::Exception((description),more) {} explicit class_name(const std::string &more) : G::Exception((description),more) {} class_name(const std::string &more1,const std::string &more2) : G::Exception((description),more1,more2) {} class_name(const std::string &more1,const std::string &more2,const std::string &more3) : G::Exception((description),more1,more2,more3) {} }
73
74#define G_EXCEPTION_FUNCTION( name , description ) \
75 inline static G::Exception name() { return G::Exception((description)) ; } \
76 inline static G::Exception name( const std::string & s ) { return G::Exception((description),s) ; } \
77 inline static G::Exception name( const std::string & s1 , const std::string & s2 ) { return G::Exception((description),s1,s2) ; } \
78 inline static G::Exception name( const std::string & s1 , const std::string & s2 , const std::string & s3 ) { return G::Exception((description),s1,s2,s3) ; }
79
80// as a code-size optimisation G_EXCEPTION can be implemented as inline
81// functions using G_EXCEPTION_FUNCION rather than separate classes
82// with G_EXCEPTION_CLASS -- G_EXCEPTION_CLASS is required when catching
83// specific errors
84//
85#ifndef G_EXCEPTION
86#define G_EXCEPTION( class_name , description ) G_EXCEPTION_FUNCTION( class_name , description )
87#endif
88
89#endif
G::Exception
A general-purpose exception class derived from std::exception and containing an error message.
Definition: gexception.h:45
G::Exception::Exception
Exception(const char *what)
Constructor.
Definition: gexception.cpp:61
G
Low-level classes.
Definition: galign.h:28