blob: 1fa7083d5ad69c983dc97c2a173e94dffe15e91a [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Tracy Narine6440ce82023-09-20 14:19:07 +01002// Copyright © 2017-2023 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
Matthew Bentham313e1c82019-03-25 17:37:47 +00007#include <sstream>
telsoa014fcda012018-03-09 14:13:49 +00008#include <stdexcept>
9#include <string>
10
11namespace armnn
12{
13
telsoa01c577f2c2018-08-31 09:22:23 +010014struct 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.
telsoa014fcda012018-03-09 14:13:49 +000046class Exception : public std::exception
47{
48public:
49 explicit Exception(const std::string& message);
50
Ryan OShea2bbfaa72020-02-12 16:15:27 +000051 /// exception with context
David Beck3cc9a622018-10-12 10:38:31 +010052 explicit Exception(const std::string& message,
53 const CheckLocation& location);
54
Ryan OShea2bbfaa72020-02-12 16:15:27 +000055 /// preserving previous exception context
56 /// and adding local context information
David Beck3cc9a622018-10-12 10:38:31 +010057 explicit Exception(const Exception& other,
58 const std::string& message,
59 const CheckLocation& location);
60
telsoa014fcda012018-03-09 14:13:49 +000061 virtual const char* what() const noexcept override;
62
63private:
64 std::string m_Message;
65};
66
Matthew Bentham9a61fa62020-02-04 10:03:55 +000067/// Class for non-fatal exceptions raised while initialising a backend
68class BackendUnavailableException : public Exception
telsoa014fcda012018-03-09 14:13:49 +000069{
70public:
71 using Exception::Exception;
72};
73
Matthew Bentham9a61fa62020-02-04 10:03:55 +000074class ClRuntimeUnavailableException : public BackendUnavailableException
75{
76public:
77 using BackendUnavailableException::BackendUnavailableException;
78};
79
telsoa014fcda012018-03-09 14:13:49 +000080class InvalidArgumentException : public Exception
81{
82public:
83 using Exception::Exception;
84};
85
86class FileNotFoundException : public Exception
87{
88public:
89 using Exception::Exception;
90};
91
92class ParseException : public Exception
93{
94public:
95 using Exception::Exception;
96};
97
98class UnimplementedException : public Exception
99{
100public:
101 using Exception::Exception;
102 UnimplementedException();
103};
104
105class LayerValidationException : public Exception
106{
107 using Exception::Exception;
108};
109
110class GraphValidationException : public Exception
111{
112 using Exception::Exception;
113};
114
David Beck5eec11d2018-10-04 15:43:17 +0100115class BadOptionalAccessException : public Exception
116{
117 using Exception::Exception;
118};
119
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100120class RuntimeException : public Exception
121{
122 using Exception::Exception;
123};
124
David Monahan4f1e8e42019-09-04 09:22:10 +0100125class MemoryImportException : public Exception
126{
127 using Exception::Exception;
128};
129
130class MemoryExportException : public Exception
131{
132 using Exception::Exception;
133};
134
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100135class TimeoutException : public Exception
136{
137 using Exception::Exception;
138};
139
Derek Lamberti4d4e0e22020-02-19 13:30:47 +0000140class PolymorphicDowncastException : public Exception
141{
142public:
143 using Exception::Exception;
144};
145
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +0100146class NullPointerException : public Exception
147{
148public:
149 using Exception::Exception;
150};
Derek Lamberti4d4e0e22020-02-19 13:30:47 +0000151
Finn Williamsdbf5f312021-08-26 11:08:01 +0100152class BackendCapabilityException : public Exception
153{
154public:
155 using Exception::Exception;
156};
157
Jim Flynne1fdd282021-10-26 21:26:10 +0100158class MemoryValidationException : public Exception
159{
160public:
161 using Exception::Exception;
162};
163
telsoa014fcda012018-03-09 14:13:49 +0000164template <typename ExceptionType>
165void ConditionalThrow(bool condition, const std::string& message)
166{
167 if (!condition)
168 {
169 throw ExceptionType(message);
170 }
171}
172
Derek Lamberti4d4e0e22020-02-19 13:30:47 +0000173template <typename ExceptionType>
174void ConditionalThrow(bool condition)
175{
176 if (!condition)
177 {
178 throw ExceptionType();
179 }
180}
181
182
surmeh013537c2c2018-05-18 16:31:43 +0100183///
184/// ComparedType must support:
185/// operator==(const ComparedType&)
186/// operator<<(ostream&, const ComparedType&)
187///
188template <typename ExceptionType, typename ComparedType>
189void 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
telsoa01c577f2c2018-08-31 09:22:23 +0100201} // namespace armnn
202
203#define CHECK_LOCATION() armnn::CheckLocation(__func__, __FILE__, __LINE__)
Tracy Narine6440ce82023-09-20 14:19:07 +0100204
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)