1#pragma once
2
3#include <stdexcept>
4
5#include <c10/util/Optional.h>
6#include <torch/csrc/Export.h>
7#include <string>
8
9namespace torch {
10namespace jit {
11
12struct TORCH_API JITException : public std::runtime_error {
13 explicit JITException(
14 const std::string& msg,
15 c10::optional<std::string> python_class_name = c10::nullopt,
16 c10::optional<std::string> original_msg = c10::nullopt);
17
18 c10::optional<std::string> getPythonClassName() const {
19 return python_class_name_;
20 }
21
22 // the original msg if this is from a python exception. The interpretor has
23 // changed the original message by adding "The following operation failed in
24 // the TorchScript interpreter." in front of it in the handleError function.
25 c10::optional<std::string> getOriginalMsg() const {
26 return original_msg_;
27 }
28
29 static const std::string& getCaughtOriginalMsg();
30 static const std::string& getCaughtPythonClassName();
31 static void setCaughtOriginalMsg(const std::string& msg);
32 static void setCaughtPythonClassName(const std::string& pythonClassName);
33
34 private:
35 c10::optional<std::string> python_class_name_;
36 c10::optional<std::string> original_msg_;
37};
38
39} // namespace jit
40} // namespace torch
41