/src/rdkit/Code/RDGeneral/Exceptions.h
Line | Count | Source |
1 | | // |
2 | | // Copyright (c) 2003-2005 greg Landrum and Rational Discovery LLC |
3 | | // |
4 | | // @@ All Rights Reserved @@ |
5 | | // This file is part of the RDKit. |
6 | | // The contents are covered by the terms of the BSD license |
7 | | // which is included in the file license.txt, found at the root |
8 | | // of the RDKit source tree. |
9 | | // |
10 | | #include <RDGeneral/export.h> |
11 | | #ifndef _RD_EXCEPTIONS_H |
12 | | #define _RD_EXCEPTIONS_H |
13 | | #include <stdexcept> |
14 | | #include <string> |
15 | | #include <string_view> |
16 | | #include <utility> |
17 | | |
18 | | //! \brief Class to allow us to throw an \c IndexError from C++ and have |
19 | | //! it make it back to Python |
20 | | //! |
21 | | class RDKIT_RDGENERAL_EXPORT IndexErrorException : public std::runtime_error { |
22 | | public: |
23 | | IndexErrorException(int i) |
24 | 0 | : std::runtime_error("IndexErrorException"), |
25 | 0 | _idx(i), |
26 | 0 | _msg("Index Error: " + std::to_string(_idx)) {} |
27 | 0 | int index() const { return _idx; } |
28 | | |
29 | 0 | const char *what() const noexcept override { return _msg.c_str(); } |
30 | | |
31 | 0 | ~IndexErrorException() noexcept override = default; |
32 | | |
33 | | private: |
34 | | int _idx; |
35 | | std::string _msg; |
36 | | }; |
37 | | |
38 | | //! \brief Class to allow us to throw a \c ValueError from C++ and have |
39 | | //! it make it back to Python |
40 | | //! |
41 | | class RDKIT_RDGENERAL_EXPORT ValueErrorException : public std::runtime_error { |
42 | | public: |
43 | | ValueErrorException(std::string i) |
44 | 25 | : std::runtime_error("ValueErrorException"), _value(std::move(i)) {} |
45 | | ValueErrorException(const char *msg) |
46 | 71 | : std::runtime_error("ValueErrorException"), _value(msg) {} |
47 | 0 | const char *what() const noexcept override { return _value.c_str(); } |
48 | 96 | ~ValueErrorException() noexcept override = default; |
49 | | |
50 | | private: |
51 | | std::string _value; |
52 | | }; |
53 | | |
54 | | //! \brief Class to allow us to throw a \c KeyError from C++ and have |
55 | | //! it make it back to Python |
56 | | //! |
57 | | class RDKIT_RDGENERAL_EXPORT KeyErrorException : public std::runtime_error { |
58 | | public: |
59 | | KeyErrorException(std::string key) |
60 | | : std::runtime_error("KeyErrorException"), |
61 | | _key(key), |
62 | 0 | _msg("Key Error: " + key) {} |
63 | | KeyErrorException(std::string_view key) |
64 | 5 | : std::runtime_error("KeyErrorException"), |
65 | 5 | _key(key), |
66 | 5 | _msg("Key Error: " + _key) {} |
67 | 0 | std::string key() const { return _key; } |
68 | | |
69 | 0 | const char *what() const noexcept override { return _msg.c_str(); } |
70 | | |
71 | 5 | ~KeyErrorException() noexcept override = default; |
72 | | |
73 | | private: |
74 | | std::string _key; |
75 | | std::string _msg; |
76 | | }; |
77 | | |
78 | | #endif |