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 a solver's failure to converge, to be thrown 72 : * by "libmesh_convergence_failure();" This should be a last 73 : * resort; more often, a solve which has failed should be 74 : * reattempted after switching to a smaller timestep, adding 75 : * underrelaxation, taking a smaller continuation step, etc. 76 : */ 77 : class ConvergenceFailure : public std::runtime_error 78 : { 79 : public: 80 179866 : ConvergenceFailure() : std::runtime_error( "Unrecoverable failure to converge" ) {} 81 : }; 82 : 83 : 84 : /** 85 : * A class representing that a dynamic cast failed to produce expected output. 86 : */ 87 : class DynamicCastFailure: public std::runtime_error 88 : { 89 : public: 90 : DynamicCastFailure() : std::runtime_error( "Failed dynamic cast!" ) {} 91 : }; 92 : 93 : /** 94 : * A class representing a floating point exception. 95 : */ 96 : class FloatingPointException: public std::runtime_error 97 : { 98 : public: 99 : FloatingPointException() : std::runtime_error( "libmesh FPE!" ) {} 100 : }; 101 : 102 : /** 103 : * A class representing an exception during a solve. 104 : */ 105 : class SolverException: public std::exception 106 : { 107 : public: 108 0 : SolverException(int error_code_in) : 109 : std::exception(), 110 0 : error_code(error_code_in) 111 : { 112 0 : std::ostringstream oss; 113 0 : oss << "Error code " << error_code << " during solve." << std::endl; 114 0 : what_message = oss.str(); 115 0 : } 116 : 117 : /** 118 : * Virtual destructor, gotta have one of those. 119 : */ 120 0 : virtual ~SolverException() = default; 121 : 122 : /** 123 : * Override the what() function to provide a generic error message. 124 : */ 125 0 : 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 0 : return what_message.c_str(); 130 : } 131 : 132 : /** 133 : * The error code generated by the solver. 134 : */ 135 : int error_code; 136 : 137 : /** 138 : * string which holds the message built in the constructor. 139 : */ 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