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"), _idx(i){};
23  int index() const { return _idx; };
24 
25  const char* what() const noexcept override {
26  std::string msg{"Index Error: "};
27  msg.append(std::to_string(_idx));
28  return msg.c_str();
29  };
30 
31  ~IndexErrorException() noexcept {};
32 
33  private:
34  int _idx;
35 };
36 
37 //! \brief Class to allow us to throw a \c ValueError from C++ and have
38 //! it make it back to Python
39 //!
40 class ValueErrorException : public std::runtime_error {
41  public:
42  ValueErrorException(const std::string& i)
43  : std::runtime_error("ValueErrorException"), _value(i){};
44  ValueErrorException(const char* msg)
45  : std::runtime_error("ValueErrorException"), _value(msg){};
46  const char* what() const noexcept override { return _value.c_str(); };
47  const char* message() const noexcept { return what(); };
48  ~ValueErrorException() noexcept {};
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 KeyErrorException : public std::runtime_error {
58  public:
59  KeyErrorException(std::string key)
60  : std::runtime_error("KeyErrorException"), _key(key){};
61  std::string key() const { return _key; };
62 
63  const char* what() const noexcept override {
64  std::string msg{"Key Error: "};
65  msg.append(_key);
66  return msg.c_str();
67  };
68 
69  ~KeyErrorException() noexcept {};
70 
71  private:
72  std::string _key;
73 };
74 
75 #endif
ValueErrorException::message
const char * message() const noexcept
Definition: Exceptions.h:47
KeyErrorException::key
std::string key() const
Definition: Exceptions.h:61
ValueErrorException::~ValueErrorException
~ValueErrorException() noexcept
Definition: Exceptions.h:48
KeyErrorException
Class to allow us to throw a KeyError from C++ and have it make it back to Python.
Definition: Exceptions.h:57
ValueErrorException::what
const char * what() const noexcept override
Definition: Exceptions.h:46
ValueErrorException
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition: Exceptions.h:40
IndexErrorException::what
const char * what() const noexcept override
Definition: Exceptions.h:25
IndexErrorException::index
int index() const
Definition: Exceptions.h:23
IndexErrorException
Class to allow us to throw an IndexError from C++ and have it make it back to Python.
Definition: Exceptions.h:19
KeyErrorException::what
const char * what() const noexcept override
Definition: Exceptions.h:63
IndexErrorException::~IndexErrorException
~IndexErrorException() noexcept
Definition: Exceptions.h:31
IndexErrorException::IndexErrorException
IndexErrorException(int i)
Definition: Exceptions.h:21
ValueErrorException::ValueErrorException
ValueErrorException(const char *msg)
Definition: Exceptions.h:44
KeyErrorException::~KeyErrorException
~KeyErrorException() noexcept
Definition: Exceptions.h:69
KeyErrorException::KeyErrorException
KeyErrorException(std::string key)
Definition: Exceptions.h:59
ValueErrorException::ValueErrorException
ValueErrorException(const std::string &i)
Definition: Exceptions.h:42
export.h