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