libMesh
libmesh_exceptions.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 <stdexcept>
26 #include <string>
27 #include <sstream>
28 
29 namespace libMesh {
30 
35 class LogicError : public std::logic_error
36 {
37 public:
38  LogicError() : std::logic_error( "Error in libMesh internal logic" ) {}
39  LogicError(const std::string & msg) : std::logic_error( msg ) {}
40 };
41 
42 
48 class NotImplemented : public std::logic_error
49 {
50 public:
51  NotImplemented(std::string msg="") : std::logic_error( "Error: feature not implemented!\n" + msg ) {}
52 };
53 
54 
62 class FileError : public std::runtime_error
63 {
64 public:
65  FileError(const std::string & filename, const std::string msg="") :
66  std::runtime_error("Error with file `" + filename + "'\n" + std::move(msg)) {}
67 };
68 
69 
77 class ConvergenceFailure : public std::runtime_error
78 {
79 public:
80  ConvergenceFailure() : std::runtime_error( "Unrecoverable failure to converge" ) {}
81 };
82 
83 
87 class DynamicCastFailure: public std::runtime_error
88 {
89 public:
90  DynamicCastFailure() : std::runtime_error( "Failed dynamic cast!" ) {}
91 };
92 
96 class FloatingPointException: public std::runtime_error
97 {
98 public:
99  FloatingPointException() : std::runtime_error( "libmesh FPE!" ) {}
100 };
101 
105 class SolverException: public std::exception
106 {
107 public:
108  SolverException(int error_code_in) :
109  std::exception(),
110  error_code(error_code_in)
111  {
112  std::ostringstream oss;
113  oss << "Error code " << error_code << " during solve." << std::endl;
114  what_message = oss.str();
115  }
116 
120  virtual ~SolverException() = default;
121 
125  virtual const char * what() const noexcept override
126  {
127  // std::string::c_str() is noexcept in C++11, so it's safe to call
128  // in what() because it can't throw.
129  return what_message.c_str();
130  }
131 
136 
140  std::string what_message;
141 };
142 
143 }
144 
145 #ifdef LIBMESH_ENABLE_EXCEPTIONS
146 #define libmesh_noexcept noexcept
147 
148 #define LIBMESH_THROW(e) do { throw e; } while (0)
149 #define libmesh_rethrow throw
150 #define libmesh_try try
151 #define libmesh_catch(e) catch(e)
152 
153 #else
154 
155 #define LIBMESH_THROW(e) do { libMesh::err << e.what(); std::abort(); } while (0)
156 #define libmesh_rethrow
157 #define libmesh_try
158 #define libmesh_catch(e) if (0)
159 
160 #endif // LIBMESH_ENABLE_EXCEPTIONS
161 
162 #endif // LIBMESH_LIBMESH_EXCEPTIONS_H
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)
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)
virtual const char * what() const noexcept override
Override the what() function to provide a generic error message.
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();"...
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...