HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exceptions.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <algorithm>
7 #include <exception>
8 #include <iterator>
9 #include <stdexcept>
10 #include <string>
11 #include <vector>
12 
13 #include "core/common/common.h"
15 
16 namespace onnxruntime {
17 
18 class NotImplementedException : public std::logic_error {
19  public:
20  explicit NotImplementedException(const char* _Message = "Function not yet implemented") noexcept : std::logic_error(_Message){};
21  explicit NotImplementedException(const std::string& _Message = "Function not yet implemented") noexcept : std::logic_error(_Message){};
22 };
23 
24 class TypeMismatchException : public std::logic_error {
25  public:
26  TypeMismatchException() noexcept : logic_error("Type mismatch"){};
27 };
28 
29 class OnnxRuntimeException : public std::exception {
30  public:
32  : OnnxRuntimeException(location, nullptr, msg) {
33  }
34 
35  /**
36  Create a new exception that captures the location it was thrown from.
37  @param location Location in the source code the exception is being thrown from
38  @param failed_condition Optional string containing the condition that failed.
39  e.g. "tensor.Size() == input.Size()". May be nullptr.
40  @param msg Message containing additional information about the exception cause.
41  */
42  OnnxRuntimeException(const CodeLocation& location, const char* failed_condition, const std::string& msg)
43  : location_{location} {
44  std::ostringstream ss;
45 
46  ss << location.ToString(CodeLocation::kFilenameAndPath); // output full path in case just the filename is ambiguous
47  if (failed_condition != nullptr) {
48  ss << " " << failed_condition << " was false.";
49  }
50 
51  ss << " " << msg << "\n";
52  if (!location.stacktrace.empty()) {
53  ss << "Stacktrace:\n";
54  // skip the first entry in the stacktrace as we have that information from location.ToString()
55  std::copy(std::next(location.stacktrace.begin()), location.stacktrace.end(), std::ostream_iterator<std::string>(ss, "\n"));
56  }
57 
58  what_ = ss.str();
59  }
60 
61  const char* what() const noexcept override {
62  return what_.c_str();
63  }
64 
65  private:
66  const CodeLocation location_;
67  const std::vector<std::string> stacktrace_;
68  std::string what_;
69 };
70 
71 } // namespace onnxruntime
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
std::string ToString(Format format=Format::kFilename) const
Definition: code_location.h:45
OnnxRuntimeException(const CodeLocation &location, const std::string &msg) noexcept
Definition: exceptions.h:31
OnnxRuntimeException(const CodeLocation &location, const char *failed_condition, const std::string &msg)
Definition: exceptions.h:42
NotImplementedException(const char *_Message="Function not yet implemented") noexcept
Definition: exceptions.h:20
GLint location
Definition: glcorearb.h:805
const char * what() const noexceptoverride
Definition: exceptions.h:61
NotImplementedException(const std::string &_Message="Function not yet implemented") noexcept
Definition: exceptions.h:21