libMesh
libmesh_exceptions.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 #ifdef LIBMESH_ENABLE_EXCEPTIONS
26 #include <stdexcept>
27 #include <string>
28 #include <sstream>
29 
30 #define libmesh_noexcept noexcept
31 
32 namespace libMesh {
33 
38 class LogicError : public std::logic_error
39 {
40 public:
41  LogicError() : std::logic_error( "Error in libMesh internal logic" ) {}
42  LogicError(const std::string & msg) : std::logic_error( msg ) {}
43 };
44 
45 
51 class NotImplemented : public std::logic_error
52 {
53 public:
54  NotImplemented() : std::logic_error( "Error: not implemented!" ) {}
55 };
56 
57 
65 class FileError : public std::runtime_error
66 {
67 public:
68  FileError(const std::string & filename) : std::runtime_error( "Error accessing file: " + filename ) {}
69 };
70 
71 
79 class ConvergenceFailure : public std::runtime_error
80 {
81 public:
82  ConvergenceFailure() : std::runtime_error( "Unrecoverable failure to converge" ) {}
83 };
84 
85 
89 class DynamicCastFailure: public std::runtime_error
90 {
91 public:
92  DynamicCastFailure() : std::runtime_error( "Failed dynamic cast!" ) {}
93 };
94 
98 class FloatingPointException: public std::runtime_error
99 {
100 public:
101  FloatingPointException() : std::runtime_error( "libmesh FPE!" ) {}
102 };
103 
107 class SolverException: public std::exception
108 {
109 public:
110  SolverException(int error_code_in) :
111  std::exception(),
112  error_code(error_code_in)
113  {
114  std::ostringstream oss;
115  oss << "Error code " << error_code << " during solve." << std::endl;
116  what_message = oss.str();
117  }
118 
122  virtual ~SolverException() noexcept {};
123 
127  virtual const char * what() const noexcept
128  {
129  // std::string::c_str() is noexcept in C++11, so it's safe to call
130  // in what() because it can't throw.
131  return what_message.c_str();
132  }
133 
138 
142  std::string what_message;
143 };
144 
145 }
146 
147 #define LIBMESH_THROW(e) do { throw e; } while (0)
148 #define libmesh_try try
149 #define libmesh_catch(e) catch(e)
150 
151 #else
152 
153 #define LIBMESH_THROW(e) do { std::abort(); } while (0)
154 #define libmesh_try
155 #define libmesh_catch(e) if (0)
156 
157 #endif // LIBMESH_ENABLE_EXCEPTIONS
158 
159 #endif // LIBMESH_LIBMESH_EXCEPTIONS_H
libMesh::FloatingPointException::FloatingPointException
FloatingPointException()
Definition: libmesh_exceptions.h:101
libMesh::FloatingPointException
A class representing a floating point exception.
Definition: libmesh_exceptions.h:98
libMesh::SolverException::what_message
std::string what_message
string which holds the message built in the constructor.
Definition: libmesh_exceptions.h:142
libMesh::DynamicCastFailure
A class representing that a dynamic cast failed to produce expected output.
Definition: libmesh_exceptions.h:89
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::LogicError
A class to represent the internal "this should never happen" errors, to be thrown by "libmesh_error()...
Definition: libmesh_exceptions.h:38
libMesh::LogicError::LogicError
LogicError()
Definition: libmesh_exceptions.h:41
libMesh::SolverException::what
virtual const char * what() const noexcept
Override the what() function to provide a generic error message.
Definition: libmesh_exceptions.h:127
libMesh::LogicError::LogicError
LogicError(const std::string &msg)
Definition: libmesh_exceptions.h:42
libMesh::FileError
A class representing a failed attempt by the library to open a file (or construct an fstream,...
Definition: libmesh_exceptions.h:65
libMesh::SolverException::error_code
int error_code
The error code generated by the solver.
Definition: libmesh_exceptions.h:137
libMesh::FileError::FileError
FileError(const std::string &filename)
Definition: libmesh_exceptions.h:68
libMesh::ConvergenceFailure::ConvergenceFailure
ConvergenceFailure()
Definition: libmesh_exceptions.h:82
libMesh::SolverException
A class representing an exception during a solve.
Definition: libmesh_exceptions.h:107
libMesh::NotImplemented::NotImplemented
NotImplemented()
Definition: libmesh_exceptions.h:54
std
Definition: float128_shims.h:27
libMesh::DynamicCastFailure::DynamicCastFailure
DynamicCastFailure()
Definition: libmesh_exceptions.h:92
libMesh::SolverException::SolverException
SolverException(int error_code_in)
Definition: libmesh_exceptions.h:110
libMesh::NotImplemented
A class to stub for features that should be in libMesh, but haven't been written yet,...
Definition: libmesh_exceptions.h:51
libMesh::ConvergenceFailure
A class representing a solver's failure to converge, to be thrown by "libmesh_convergence_failure();"...
Definition: libmesh_exceptions.h:79
libMesh::SolverException::~SolverException
virtual ~SolverException() noexcept
Virtual destructor, gotta have one of those.
Definition: libmesh_exceptions.h:122