RDKit
Open-source cheminformatics and machine learning.
Exceptions.h
Go to the documentation of this file.
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 
16 //! \brief Class to allow us to throw an \c IndexError from C++ and have
17 //! it make it back to Python
18 //!
19 class IndexErrorException : public std::runtime_error {
20  public:
22  : std::runtime_error("IndexErrorException"),
23  _idx(i),
24  _msg("Index Error: " + std::to_string(_idx)){};
25  int index() const { return _idx; };
26 
27  const char* what() const noexcept override { return _msg.c_str(); };
28 
29  ~IndexErrorException() noexcept {};
30 
31  private:
32  int _idx;
33  std::string _msg;
34 };
35 
36 //! \brief Class to allow us to throw a \c ValueError from C++ and have
37 //! it make it back to Python
38 //!
39 class ValueErrorException : public std::runtime_error {
40  public:
41  ValueErrorException(const std::string& i)
42  : std::runtime_error("ValueErrorException"), _value(i){};
43  ValueErrorException(const char* msg)
44  : std::runtime_error("ValueErrorException"), _value(msg){};
45  const char* what() const noexcept override { return _value.c_str(); };
46  ~ValueErrorException() noexcept {};
47 
48  private:
49  std::string _value;
50 };
51 
52 //! \brief Class to allow us to throw a \c KeyError from C++ and have
53 //! it make it back to Python
54 //!
55 class KeyErrorException : public std::runtime_error {
56  public:
57  KeyErrorException(std::string key)
58  : std::runtime_error("KeyErrorException"),
59  _key(key),
60  _msg("Key Error: " + key){};
61  std::string key() const { return _key; };
62 
63  const char* what() const noexcept override { return _msg.c_str(); };
64 
65  ~KeyErrorException() noexcept {};
66 
67  private:
68  std::string _key;
69  std::string _msg;
70 };
71 
72 #endif
Class to allow us to throw an IndexError from C++ and have it make it back to Python.
Definition: Exceptions.h:19
IndexErrorException(int i)
Definition: Exceptions.h:21
~IndexErrorException() noexcept
Definition: Exceptions.h:29
int index() const
Definition: Exceptions.h:25
const char * what() const noexcept override
Definition: Exceptions.h:27
Class to allow us to throw a KeyError from C++ and have it make it back to Python.
Definition: Exceptions.h:55
~KeyErrorException() noexcept
Definition: Exceptions.h:65
const char * what() const noexcept override
Definition: Exceptions.h:63
std::string key() const
Definition: Exceptions.h:61
KeyErrorException(std::string key)
Definition: Exceptions.h:57
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition: Exceptions.h:39
ValueErrorException(const char *msg)
Definition: Exceptions.h:43
~ValueErrorException() noexcept
Definition: Exceptions.h:46
const char * what() const noexcept override
Definition: Exceptions.h:45
ValueErrorException(const std::string &i)
Definition: Exceptions.h:41
static std::string to_string(const Descriptor &desc)
Definition: Descriptor.h:54