libMesh
libmesh_exceptions.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2026 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 #include "libmesh/libmesh_abort.h"
26 
27 #include <stdexcept>
28 #include <string>
29 #include <sstream>
30 
31 namespace libMesh {
32 
45 
49 void enableFPE(bool on);
50 
54 void enableSEGV(bool on);
55 
59 void enableSIGINT(bool on);
60 
65 class LogicError : public std::logic_error
66 {
67 public:
68  LogicError() : std::logic_error( "Error in libMesh internal logic" ) {}
69  LogicError(const std::string & msg) : std::logic_error( msg ) {}
70 };
71 
72 
78 class NotImplemented : public std::logic_error
79 {
80 public:
81  NotImplemented(std::string msg="") : std::logic_error( "Error: feature not implemented!\n" + msg ) {}
82 };
83 
84 
92 class FileError : public std::runtime_error
93 {
94 public:
95  FileError(const std::string & filename, const std::string msg="") :
96  std::runtime_error("Error with file `" + filename + "'\n" + std::move(msg)) {}
97 };
98 
99 
116 class DegenerateMap : public std::runtime_error
117 {
118 public:
119  DegenerateMap(std::string msg="") :
120  std::runtime_error( "Degenerate map, e.g. negative Jacobian or singular matrix.\n" + msg ) {}
121 };
122 
123 
131 class ConvergenceFailure : public std::runtime_error
132 {
133 public:
134  ConvergenceFailure(const std::string & err_msg="Unrecoverable failure to converge") : std::runtime_error( err_msg ) {}
135 };
136 
137 
141 class DynamicCastFailure: public std::runtime_error
142 {
143 public:
144  DynamicCastFailure() : std::runtime_error( "Failed dynamic cast!" ) {}
145 };
146 
150 class FloatingPointException: public std::runtime_error
151 {
152 public:
153  FloatingPointException() : std::runtime_error( "libmesh FPE!" ) {}
154 };
155 
159 class SolverException: public std::exception
160 {
161 public:
162  SolverException(int error_code_in) :
163  std::exception(),
164  error_code(error_code_in)
165  {
166  std::ostringstream oss;
167  oss << "Error code " << error_code << " during solve." << std::endl;
168  what_message = oss.str();
169  }
170 
174  virtual ~SolverException() = default;
175 
179  virtual const char * what() const noexcept override
180  {
181  // std::string::c_str() is noexcept in C++11, so it's safe to call
182  // in what() because it can't throw.
183  return what_message.c_str();
184  }
185 
190 
194  std::string what_message;
195 };
196 
208 {
209 public:
211 
212  const char * what() const noexcept { return "libMesh termination requested"; }
213 };
214 
215 }
216 
217 #ifdef LIBMESH_ENABLE_EXCEPTIONS
218 #define libmesh_noexcept noexcept
219 
220 #define LIBMESH_THROW(e) do { throw e; } while (0)
221 #define libmesh_rethrow throw
222 #define libmesh_try try
223 #define libmesh_catch(e) catch(e)
224 
225 #else
226 
227 #define LIBMESH_THROW(e) do { libMesh::err << e.what(); libMesh::libmesh_abort(); } while (0)
228 #define libmesh_rethrow
229 #define libmesh_try
230 #define libmesh_catch(e) if (0)
231 
232 #endif // LIBMESH_ENABLE_EXCEPTIONS
233 
234 #endif // LIBMESH_LIBMESH_EXCEPTIONS_H
DegenerateMap(std::string msg="")
A class to stub for features that should be in libMesh, but haven&#39;t been written yet, to be thrown by "libmesh_not_implemented();".
NotImplemented(std::string msg="")
A class representing that a dynamic cast failed to produce expected output.
SolverException(int error_code_in)
A class representing the detection of an unexpected degeneracy, e.g.
ConvergenceFailure(const std::string &err_msg="Unrecoverable failure to converge")
void enableSEGV(bool on)
Toggle libMesh reporting of segmentation faults.
int error_code
The error code generated by the solver.
A class representing a floating point exception.
The libMesh namespace provides an interface to certain functionality in the library.
std::string what_message
string which holds the message built in the constructor.
A class representing an exception during a solve.
LogicError(const std::string &msg)
A class representing an exception used only to send a program to the terminate handler for abort afte...
virtual const char * what() const noexcept override
Override the what() function to provide a generic error message.
void enableFPE(bool on)
Toggle hardware trap floating point exceptions.
FileError(const std::string &filename, const std::string msg="")
A class to represent the internal "this should never happen" errors, to be thrown by "libmesh_error()...
A class representing a solver&#39;s failure to converge, to be thrown by "libmesh_convergence_failure();"...
void enableSIGINT(bool on)
Toggle libMesh handling of SIGINT (Ctrl+C) interrupts.
virtual ~SolverException()=default
Virtual destructor, gotta have one of those.
A class representing a failed attempt by the library to open a file (or construct an fstream...
const char * what() const noexcept
void libmesh_terminate_handler()
A terminate handler.