libmesh_exceptions.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library 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 GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_LIBMESH_EXCEPTIONS_H
21 #define LIBMESH_LIBMESH_EXCEPTIONS_H
22 
23 #include "libmesh/libmesh_config.h"
24 
25 #ifdef LIBMESH_ENABLE_EXCEPTIONS
26 #include <stdexcept>
27 #include <string>
28 #include <sstream>
29 
30 #define libmesh_noexcept noexcept
31 
32 namespace libMesh {
33 
38 class LogicError : public std::logic_error
39 {
40 public:
41  LogicError() : std::logic_error( "Error in libMesh internal logic" ) {}
42  LogicError(const std::string & msg) : std::logic_error( msg ) {}
43 };
44 
45 
51 class NotImplemented : public std::logic_error
52 {
53 public:
54  NotImplemented() : std::logic_error( "Error: not implemented!" ) {}
55 };
56 
57 
65 class FileError : public std::runtime_error
66 {
67 public:
68  FileError(const std::string & filename) : std::runtime_error( "Error accessing file: " + filename ) {}
69 };
70 
71 
79 class ConvergenceFailure : public std::runtime_error
80 {
81 public:
82  ConvergenceFailure() : std::runtime_error( "Unrecoverable failure to converge" ) {}
83 };
84 
85 
89 class DynamicCastFailure: public std::runtime_error
90 {
91 public:
92  DynamicCastFailure() : std::runtime_error( "Failed dynamic cast!" ) {}
93 };
94 
98 class FloatingPointException: public std::runtime_error
99 {
100 public:
101  FloatingPointException() : std::runtime_error( "libmesh FPE!" ) {}
102 };
103 
107 class SolverException: public std::exception
108 {
109 public:
110  SolverException(int error_code_in) :
111  std::exception(),
112  error_code(error_code_in)
113  {
114  std::ostringstream oss;
115  oss << "Error code " << error_code << " during solve." << std::endl;
116  what_message = oss.str();
117  }
118 
122  virtual ~SolverException() noexcept {};
123 
127  virtual const char * what() const noexcept
128  {
129  // std::string::c_str() is noexcept in C++11, so it's safe to call
130  // in what() because it can't throw.
131  return what_message.c_str();
132  }
133 
138 
142  std::string what_message;
143 };
144 
145 }
146 
147 #define LIBMESH_THROW(e) do { throw e; } while (0)
148 #define libmesh_try try
149 #define libmesh_catch(e) catch(e)
150 
151 #else
152 
153 #define LIBMESH_THROW(e) do { std::abort(); } while (0)
154 #define libmesh_try
155 #define libmesh_catch(e) if (0)
156 
157 #endif // LIBMESH_ENABLE_EXCEPTIONS
158 
159 #endif // LIBMESH_LIBMESH_EXCEPTIONS_H
SolverException(int error_code_in)
FileError(const std::string &filename)
LogicError(const std::string &msg)
virtual const char * what() const noexcept
virtual ~SolverException() noexcept