OpenCAP
Electronic structure of metastable states
opencap_exception.h
Go to the documentation of this file.
1 /*Copyright (c) 2021 James Gayvert
2 
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9 
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12 
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE.
20 */
21 
26 #pragma once
27 
28 #include <stdexcept>
29 #include <string>
30 
34 class Exception : public std::runtime_error
35 {
36  public:
37  Exception(const std::string & message, const char * file, unsigned int line):
38  std::runtime_error(message)
39  {
40  _message = std::string(file) + ":" + std::to_string(line) + " : " + message;
41  }
42 
43  ~Exception() throw() {}
44 
45  const char * what() const throw()
46  {
47  return _message.c_str();
48  }
49 
50  private:
51  std::string _message;
52  };
53 
57 void rethrow(const std::string & message, const char * file, unsigned int line);
58 
61 void Handle_Exception(const std::exception & ex, const std::string & function);
62 
63 // Shorthand for throwing an Exception with file and line info using macros
64 #define opencap_throw(message) throw Exception(message, __FILE__, __LINE__);
65 
66 // Shorthand for rethrowing and Exception with file and line info using macros
67 #define opencap_rethrow(message) rethrow(message, __FILE__, __LINE__);
68 
69 // Shorthand for handling an exception, including a backtrace
70 #define opencap_handle_exception(ex) Handle_Exception(ex, __func__);
Class for handling nested exceptions. Adapted from: https://github.com/GPMueller/mwe-cpp-exception.
Definition: opencap_exception.h:34
const char * what() const
Definition: opencap_exception.h:45
~Exception()
Definition: opencap_exception.h:43
void rethrow(const std::string &message, const char *file, unsigned int line)
Exception(const std::string &message, const char *file, unsigned int line)
Definition: opencap_exception.h:37
void Handle_Exception(const std::exception &ex, const std::string &function)