RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
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#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//!
21class RDKIT_RDGENERAL_EXPORT IndexErrorException : public std::runtime_error {
22 public:
24 : std::runtime_error("IndexErrorException"),
25 _idx(i),
26 _msg("Index Error: " + std::to_string(_idx)) {}
27 int index() const { return _idx; }
28
29 const char *what() const noexcept override { return _msg.c_str(); }
30
31 ~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//!
41class RDKIT_RDGENERAL_EXPORT ValueErrorException : public std::runtime_error {
42 public:
43 ValueErrorException(std::string i)
44 : std::runtime_error("ValueErrorException"), _value(std::move(i)) {}
45 ValueErrorException(const char *msg)
46 : std::runtime_error("ValueErrorException"), _value(msg) {}
47 const char *what() const noexcept override { return _value.c_str(); }
48 ~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//!
57class RDKIT_RDGENERAL_EXPORT KeyErrorException : public std::runtime_error {
58 public:
60 : std::runtime_error("KeyErrorException"),
61 _key(key),
62 _msg("Key Error: " + key) {}
63 KeyErrorException(std::string_view key)
64 : std::runtime_error("KeyErrorException"),
65 _key(key),
66 _msg("Key Error: " + _key) {}
67 std::string key() const { return _key; }
68
69 const char *what() const noexcept override { return _msg.c_str(); }
70
71 ~KeyErrorException() noexcept override = default;
72
73 private:
74 std::string _key;
75 std::string _msg;
76};
77
78#endif
const char * what() const noexcept override
Definition Exceptions.h:29
IndexErrorException(int i)
Definition Exceptions.h:23
~IndexErrorException() noexcept override=default
int index() const
Definition Exceptions.h:27
KeyErrorException(std::string_view key)
Definition Exceptions.h:63
std::string key() const
Definition Exceptions.h:67
const char * what() const noexcept override
Definition Exceptions.h:69
~KeyErrorException() noexcept override=default
KeyErrorException(std::string key)
Definition Exceptions.h:59
ValueErrorException(const char *msg)
Definition Exceptions.h:45
~ValueErrorException() noexcept override=default
const char * what() const noexcept override
Definition Exceptions.h:47
ValueErrorException(std::string i)
Definition Exceptions.h:43
#define RDKIT_RDGENERAL_EXPORT
Definition export.h:449