ArmNN
 25.11
Loading...
Searching...
No Matches
Exceptions.hpp
Go to the documentation of this file.
1//
2// Copyright © 2017-2023 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
7#include <sstream>
8#include <stdexcept>
9#include <string>
10
11namespace armnn
12{
13
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.
46class Exception : public std::exception
47{
48public:
49 explicit Exception(const std::string& message);
50
51 /// exception with context
52 explicit Exception(const std::string& message,
53 const CheckLocation& location);
54
55 /// preserving previous exception context
56 /// and adding local context information
57 explicit Exception(const Exception& other,
58 const std::string& message,
59 const CheckLocation& location);
60
61 virtual const char* what() const noexcept override;
62
63private:
64 std::string m_Message;
65};
66
67/// Class for non-fatal exceptions raised while initialising a backend
69{
70public:
72};
73
75{
76public:
77 using BackendUnavailableException::BackendUnavailableException;
78};
79
81{
82public:
84};
85
87{
88public:
90};
91
93{
94public:
96};
97
99{
100public:
103};
104
105class LayerValidationException : public Exception
106{
108};
109
110class GraphValidationException : public Exception
111{
113};
114
115class BadOptionalAccessException : public Exception
116{
118};
119
120class RuntimeException : public Exception
121{
123};
124
125class MemoryImportException : public Exception
126{
128};
129
130class MemoryExportException : public Exception
131{
133};
134
135class TimeoutException : public Exception
136{
138};
139
141{
142public:
144};
145
147{
148public:
150};
151
153{
154public:
156};
157
159{
160public:
162};
163
164template <typename ExceptionType>
165void ConditionalThrow(bool condition, const std::string& message)
166{
167 if (!condition)
168 {
169 throw ExceptionType(message);
170 }
171}
172
173template <typename ExceptionType>
174void ConditionalThrow(bool condition)
175{
176 if (!condition)
177 {
178 throw ExceptionType();
179 }
180}
181
182
183///
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
201} // namespace armnn
202
203#define CHECK_LOCATION() armnn::CheckLocation(__func__, __FILE__, __LINE__)
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)
Exception(const std::string &message)
Class for non-fatal exceptions raised while initialising a backend.
Exception(const std::string &message)
virtual const char * what() const noexcept override
Exception(const std::string &message)
Exception(const std::string &message)
Exception(const std::string &message)
Exception(const std::string &message)
Exception(const std::string &message)
Exception(const std::string &message)
Exception(const std::string &message)
Exception(const std::string &message)
Copyright (c) 2021 ARM Limited and Contributors.
void ConditionalThrowIfNotEqual(const std::string &message, const ComparedType &leftHandSide, const ComparedType &rightHandSide)
ComparedType must support: operator==(const ComparedType&) operator<<(ostream&, const ComparedType&)
void ConditionalThrow(bool condition, const std::string &message)
const char * m_Function
std::string AsString() const
CheckLocation(const char *func, const char *file, unsigned int line)
const char * m_File
std::string FileLine() const
unsigned int m_Line