ArmNN
 21.02
Exceptions.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 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 
11 namespace 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.
46 class Exception : public std::exception
47 {
48 public:
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 
63 private:
64  std::string m_Message;
65 };
66 
67 /// Class for non-fatal exceptions raised while initialising a backend
69 {
70 public:
72 };
73 
75 {
76 public:
77  using BackendUnavailableException::BackendUnavailableException;
78 };
79 
81 {
82 public:
84 };
85 
87 {
88 public:
90 };
91 
92 class ParseException : public Exception
93 {
94 public:
96 };
97 
99 {
100 public:
101  using Exception::Exception;
103 };
104 
106 {
107  using Exception::Exception;
108 };
109 
111 {
112  using Exception::Exception;
113 };
114 
116 {
117  using Exception::Exception;
118 };
119 
121 {
122  using Exception::Exception;
123 };
124 
126 {
127  using Exception::Exception;
128 };
129 
131 {
132  using Exception::Exception;
133 };
134 
136 {
137  using Exception::Exception;
138 };
139 
141 {
142 public:
143  using Exception::Exception;
144 };
145 
147 {
148 public:
149  using Exception::Exception;
150 };
151 
153 {
154 public:
155  using Exception::Exception;
156 };
157 
158 template <typename ExceptionType>
159 void ConditionalThrow(bool condition, const std::string& message)
160 {
161  if (!condition)
162  {
163  throw ExceptionType(message);
164  }
165 }
166 
167 template <typename ExceptionType>
168 void ConditionalThrow(bool condition)
169 {
170  if (!condition)
171  {
172  throw ExceptionType();
173  }
174 }
175 
176 
177 ///
178 /// ComparedType must support:
179 /// operator==(const ComparedType&)
180 /// operator<<(ostream&, const ComparedType&)
181 ///
182 template <typename ExceptionType, typename ComparedType>
183 void ConditionalThrowIfNotEqual(const std::string& message,
184  const ComparedType& leftHandSide,
185  const ComparedType& rightHandSide)
186 {
187  if (!(leftHandSide == rightHandSide))
188  {
189  std::stringstream ss;
190  ss << message << " : " << leftHandSide << " != " << rightHandSide;
191  throw ExceptionType(ss.str());
192  }
193 }
194 
195 } // namespace armnn
196 
197 #define CHECK_LOCATION() armnn::CheckLocation(__func__, __FILE__, __LINE__)
void ConditionalThrowIfNotEqual(const std::string &message, const ComparedType &leftHandSide, const ComparedType &rightHandSide)
ComparedType must support: operator==(const ComparedType&) operator<<(ostream&, const ComparedType&) ...
Definition: Exceptions.hpp:183
std::string AsString() const
Definition: Exceptions.hpp:29
Exception(const std::string &message)
Definition: Exceptions.cpp:12
Copyright (c) 2021 ARM Limited and Contributors.
unsigned int m_Line
Definition: Exceptions.hpp:18
void ConditionalThrow(bool condition, const std::string &message)
Definition: Exceptions.hpp:159
std::string FileLine() const
Definition: Exceptions.hpp:37
const char * m_File
Definition: Exceptions.hpp:17
CheckLocation(const char *func, const char *file, unsigned int line)
Definition: Exceptions.hpp:20
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
const char * m_Function
Definition: Exceptions.hpp:16
Class for non-fatal exceptions raised while initialising a backend.
Definition: Exceptions.hpp:68