Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Tracy Narine | 6440ce8 | 2023-09-20 14:19:07 +0100 | [diff] [blame] | 2 | // Copyright © 2017-2023 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
Matthew Bentham | 313e1c8 | 2019-03-25 17:37:47 +0000 | [diff] [blame] | 7 | #include <sstream> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | #include <stdexcept> |
| 9 | #include <string> |
| 10 | |
| 11 | namespace armnn |
| 12 | { |
| 13 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 14 | struct CheckLocation |
| 15 | { |
| 16 | const char* m_Function; |
| 17 | const char* m_File; |
| 18 | unsigned int m_Line; |
| 19 | |
| 20 | CheckLocation(const char* func, |
| 21 | const char* file, |
| 22 | unsigned int line) |
| 23 | : m_Function{func} |
| 24 | , m_File{file} |
| 25 | , m_Line{line} |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | std::string AsString() const |
| 30 | { |
| 31 | std::stringstream ss; |
| 32 | ss << " at function " << m_Function |
| 33 | << " [" << m_File << ':' << m_Line << "]"; |
| 34 | return ss.str(); |
| 35 | } |
| 36 | |
| 37 | std::string FileLine() const |
| 38 | { |
| 39 | std::stringstream ss; |
| 40 | ss << " [" << m_File << ':' << m_Line << "]"; |
| 41 | return ss.str(); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | /// Base class for all ArmNN exceptions so that users can filter to just those. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | class Exception : public std::exception |
| 47 | { |
| 48 | public: |
| 49 | explicit Exception(const std::string& message); |
| 50 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 51 | /// exception with context |
David Beck | 3cc9a62 | 2018-10-12 10:38:31 +0100 | [diff] [blame] | 52 | explicit Exception(const std::string& message, |
| 53 | const CheckLocation& location); |
| 54 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 55 | /// preserving previous exception context |
| 56 | /// and adding local context information |
David Beck | 3cc9a62 | 2018-10-12 10:38:31 +0100 | [diff] [blame] | 57 | explicit Exception(const Exception& other, |
| 58 | const std::string& message, |
| 59 | const CheckLocation& location); |
| 60 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | virtual const char* what() const noexcept override; |
| 62 | |
| 63 | private: |
| 64 | std::string m_Message; |
| 65 | }; |
| 66 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 67 | /// Class for non-fatal exceptions raised while initialising a backend |
| 68 | class BackendUnavailableException : public Exception |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 69 | { |
| 70 | public: |
| 71 | using Exception::Exception; |
| 72 | }; |
| 73 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 74 | class ClRuntimeUnavailableException : public BackendUnavailableException |
| 75 | { |
| 76 | public: |
| 77 | using BackendUnavailableException::BackendUnavailableException; |
| 78 | }; |
| 79 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 80 | class InvalidArgumentException : public Exception |
| 81 | { |
| 82 | public: |
| 83 | using Exception::Exception; |
| 84 | }; |
| 85 | |
| 86 | class FileNotFoundException : public Exception |
| 87 | { |
| 88 | public: |
| 89 | using Exception::Exception; |
| 90 | }; |
| 91 | |
| 92 | class ParseException : public Exception |
| 93 | { |
| 94 | public: |
| 95 | using Exception::Exception; |
| 96 | }; |
| 97 | |
| 98 | class UnimplementedException : public Exception |
| 99 | { |
| 100 | public: |
| 101 | using Exception::Exception; |
| 102 | UnimplementedException(); |
| 103 | }; |
| 104 | |
| 105 | class LayerValidationException : public Exception |
| 106 | { |
| 107 | using Exception::Exception; |
| 108 | }; |
| 109 | |
| 110 | class GraphValidationException : public Exception |
| 111 | { |
| 112 | using Exception::Exception; |
| 113 | }; |
| 114 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 115 | class BadOptionalAccessException : public Exception |
| 116 | { |
| 117 | using Exception::Exception; |
| 118 | }; |
| 119 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 120 | class RuntimeException : public Exception |
| 121 | { |
| 122 | using Exception::Exception; |
| 123 | }; |
| 124 | |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 125 | class MemoryImportException : public Exception |
| 126 | { |
| 127 | using Exception::Exception; |
| 128 | }; |
| 129 | |
| 130 | class MemoryExportException : public Exception |
| 131 | { |
| 132 | using Exception::Exception; |
| 133 | }; |
| 134 | |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 135 | class TimeoutException : public Exception |
| 136 | { |
| 137 | using Exception::Exception; |
| 138 | }; |
| 139 | |
Derek Lamberti | 4d4e0e2 | 2020-02-19 13:30:47 +0000 | [diff] [blame] | 140 | class PolymorphicDowncastException : public Exception |
| 141 | { |
| 142 | public: |
| 143 | using Exception::Exception; |
| 144 | }; |
| 145 | |
Narumol Prangnawarat | 3b90af6 | 2020-06-26 11:00:21 +0100 | [diff] [blame] | 146 | class NullPointerException : public Exception |
| 147 | { |
| 148 | public: |
| 149 | using Exception::Exception; |
| 150 | }; |
Derek Lamberti | 4d4e0e2 | 2020-02-19 13:30:47 +0000 | [diff] [blame] | 151 | |
Finn Williams | dbf5f31 | 2021-08-26 11:08:01 +0100 | [diff] [blame] | 152 | class BackendCapabilityException : public Exception |
| 153 | { |
| 154 | public: |
| 155 | using Exception::Exception; |
| 156 | }; |
| 157 | |
Jim Flynn | e1fdd28 | 2021-10-26 21:26:10 +0100 | [diff] [blame] | 158 | class MemoryValidationException : public Exception |
| 159 | { |
| 160 | public: |
| 161 | using Exception::Exception; |
| 162 | }; |
| 163 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 164 | template <typename ExceptionType> |
| 165 | void ConditionalThrow(bool condition, const std::string& message) |
| 166 | { |
| 167 | if (!condition) |
| 168 | { |
| 169 | throw ExceptionType(message); |
| 170 | } |
| 171 | } |
| 172 | |
Derek Lamberti | 4d4e0e2 | 2020-02-19 13:30:47 +0000 | [diff] [blame] | 173 | template <typename ExceptionType> |
| 174 | void ConditionalThrow(bool condition) |
| 175 | { |
| 176 | if (!condition) |
| 177 | { |
| 178 | throw ExceptionType(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 183 | /// |
| 184 | /// ComparedType must support: |
| 185 | /// operator==(const ComparedType&) |
| 186 | /// operator<<(ostream&, const ComparedType&) |
| 187 | /// |
| 188 | template <typename ExceptionType, typename ComparedType> |
| 189 | void ConditionalThrowIfNotEqual(const std::string& message, |
| 190 | const ComparedType& leftHandSide, |
| 191 | const ComparedType& rightHandSide) |
| 192 | { |
| 193 | if (!(leftHandSide == rightHandSide)) |
| 194 | { |
| 195 | std::stringstream ss; |
| 196 | ss << message << " : " << leftHandSide << " != " << rightHandSide; |
| 197 | throw ExceptionType(ss.str()); |
| 198 | } |
| 199 | } |
| 200 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 201 | } // namespace armnn |
| 202 | |
| 203 | #define CHECK_LOCATION() armnn::CheckLocation(__func__, __FILE__, __LINE__) |
Tracy Narine | 6440ce8 | 2023-09-20 14:19:07 +0100 | [diff] [blame] | 204 | |
| 205 | // Use to throw rather than assert |
| 206 | #define ARMNN_THROW_MSG_IF_FALSE(_cond, _except, _str) \ |
| 207 | do { if (!(static_cast<bool>(_cond))) {throw _except(_str);} } while(0) |
| 208 | #define ARMNN_THROW_IF_FALSE(_cond, _except) \ |
| 209 | ARMNN_THROW_MSG_IF_FALSE(_cond, _except, #_cond) |
| 210 | #define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str) \ |
| 211 | ARMNN_THROW_MSG_IF_FALSE(_cond, armnn::InvalidArgumentException, _str) |
| 212 | #define ARMNN_THROW_INVALIDARG_IF_FALSE(_cond) \ |
| 213 | ARMNN_THROW_MSG_IF_FALSE(_cond, armnn::InvalidArgumentException, #_cond) |