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