Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2025 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 : 31 : /** 32 : * A class to represent the internal "this should never happen" 33 : * errors, to be thrown by "libmesh_error();" 34 : */ 35 : class LogicError : public std::logic_error 36 : { 37 : public: 38 : LogicError() : std::logic_error( "Error in libMesh internal logic" ) {} 39 1585 : LogicError(const std::string & msg) : std::logic_error( msg ) {} 40 : }; 41 : 42 : 43 : /** 44 : * A class to stub for features that should be in libMesh, but 45 : * haven't been written yet, to be thrown by 46 : * "libmesh_not_implemented();" 47 : */ 48 : class NotImplemented : public std::logic_error 49 : { 50 : public: 51 0 : NotImplemented(std::string msg="") : std::logic_error( "Error: feature not implemented!\n" + msg ) {} 52 : }; 53 : 54 : 55 : /** 56 : * A class representing a failed attempt by the library to open a 57 : * file (or construct an fstream, etc), to be thrown by 58 : * "libmesh_file_error(filename);" For ease of debugging, "filename" 59 : * should include any (absolute or relative or implicit) pathname 60 : * that was part of the failed open. 61 : */ 62 : class FileError : public std::runtime_error 63 : { 64 : public: 65 0 : FileError(const std::string & filename, const std::string msg="") : 66 0 : std::runtime_error("Error with file `" + filename + "'\n" + std::move(msg)) {} 67 : }; 68 : 69 : 70 : /** 71 : * A class representing the detection of a point xi in an element's 72 : * "master space" at which the mapping to physical space has a 73 : * too-small (negative, or zero, or nearly zero) Jacobian determinant, 74 : * where "too-small" is determined by a particular library method's 75 : * assigned tolerance. 76 : */ 77 : class DegenerateMapping : public std::runtime_error 78 : { 79 : public: 80 0 : DegenerateMapping(std::string msg="") : 81 0 : std::runtime_error( "Problem with mapping or its Jacobian.\n" + msg ) {} 82 : }; 83 : 84 : 85 : /** 86 : * A class representing a solver's failure to converge, to be thrown 87 : * by "libmesh_convergence_failure();" This should be a last 88 : * resort; more often, a solve which has failed should be 89 : * reattempted after switching to a smaller timestep, adding 90 : * underrelaxation, taking a smaller continuation step, etc. 91 : */ 92 : class ConvergenceFailure : public std::runtime_error 93 : { 94 : public: 95 179866 : ConvergenceFailure() : std::runtime_error( "Unrecoverable failure to converge" ) {} 96 : }; 97 : 98 : 99 : /** 100 : * A class representing that a dynamic cast failed to produce expected output. 101 : */ 102 : class DynamicCastFailure: public std::runtime_error 103 : { 104 : public: 105 : DynamicCastFailure() : std::runtime_error( "Failed dynamic cast!" ) {} 106 : }; 107 : 108 : /** 109 : * A class representing a floating point exception. 110 : */ 111 : class FloatingPointException: public std::runtime_error 112 : { 113 : public: 114 : FloatingPointException() : std::runtime_error( "libmesh FPE!" ) {} 115 : }; 116 : 117 : /** 118 : * A class representing an exception during a solve. 119 : */ 120 : class SolverException: public std::exception 121 : { 122 : public: 123 0 : SolverException(int error_code_in) : 124 : std::exception(), 125 0 : error_code(error_code_in) 126 : { 127 0 : std::ostringstream oss; 128 0 : oss << "Error code " << error_code << " during solve." << std::endl; 129 0 : what_message = oss.str(); 130 0 : } 131 : 132 : /** 133 : * Virtual destructor, gotta have one of those. 134 : */ 135 0 : virtual ~SolverException() = default; 136 : 137 : /** 138 : * Override the what() function to provide a generic error message. 139 : */ 140 0 : virtual const char * what() const noexcept override 141 : { 142 : // std::string::c_str() is noexcept in C++11, so it's safe to call 143 : // in what() because it can't throw. 144 0 : return what_message.c_str(); 145 : } 146 : 147 : /** 148 : * The error code generated by the solver. 149 : */ 150 : int error_code; 151 : 152 : /** 153 : * string which holds the message built in the constructor. 154 : */ 155 : std::string what_message; 156 : }; 157 : 158 : } 159 : 160 : #ifdef LIBMESH_ENABLE_EXCEPTIONS 161 : #define libmesh_noexcept noexcept 162 : 163 : #define LIBMESH_THROW(e) do { throw e; } while (0) 164 : #define libmesh_rethrow throw 165 : #define libmesh_try try 166 : #define libmesh_catch(e) catch(e) 167 : 168 : #else 169 : 170 : #define LIBMESH_THROW(e) do { libMesh::err << e.what(); std::abort(); } while (0) 171 : #define libmesh_rethrow 172 : #define libmesh_try 173 : #define libmesh_catch(e) if (0) 174 : 175 : #endif // LIBMESH_ENABLE_EXCEPTIONS 176 : 177 : #endif // LIBMESH_LIBMESH_EXCEPTIONS_H