From e271d85d4e047ce0b9e39af1548778a8e23a258a Mon Sep 17 00:00:00 2001 From: Yangmei Lin Date: Wed, 29 Mar 2023 21:31:52 -0700 Subject: [PATCH] Convert multipy embedded library exceptions to specific exception type (#313) Summary: Pull Request resolved: https://github.com/pytorch/multipy/pull/313 Convert multipy embedded library exceptions to specific exception type Reviewed By: PaliC Differential Revision: D44477860 fbshipit-source-id: 9cb3d1f17f54a61a79ed9766bcd9377d76a1aea8 --- multipy/runtime/Exception.h | 13 +++++++++++++ multipy/runtime/interpreter/interpreter_impl.cpp | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/multipy/runtime/Exception.h b/multipy/runtime/Exception.h index 2fa9fd5c..0e31a2da 100644 --- a/multipy/runtime/Exception.h +++ b/multipy/runtime/Exception.h @@ -4,10 +4,13 @@ // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. +#pragma once + #ifndef MULTIPY_EXCEPTION_H #define MULTIPY_EXCEPTION_H #include +#include #define MULTIPY_INTERNAL_ASSERT_WITH_MESSAGE(condition, message) \ if (!(condition)) { \ @@ -51,3 +54,13 @@ MULTIPY_CHECK_NO_MESSAGE(__VA_ARGS__)); #endif // MULTIPY_EXCEPTION_H + +namespace torch { +namespace deploy { +class MultipyEmbeddedException : public std::runtime_error { + public: + explicit MultipyEmbeddedException(const std::string& error) + : std::runtime_error(error) {} +}; +} // namespace deploy +} // namespace torch diff --git a/multipy/runtime/interpreter/interpreter_impl.cpp b/multipy/runtime/interpreter/interpreter_impl.cpp index 76e7ace0..f1f2a279 100644 --- a/multipy/runtime/interpreter/interpreter_impl.cpp +++ b/multipy/runtime/interpreter/interpreter_impl.cpp @@ -92,17 +92,17 @@ class MultiPySafeRethrow { auto code = err.value().attr("code").cast(); std::exit(code); } - throw std::runtime_error( + throw torch::deploy::MultipyEmbeddedException( std::string(file_) + ":" + std::to_string(line_) + ": Exception Caught inside torch::deploy embedded library: \n" + err.what()); } catch (std::exception& err) { - throw std::runtime_error( + throw torch::deploy::MultipyEmbeddedException( std::string(file_) + ":" + std::to_string(line_) + ": Exception Caught inside torch::deploy embedded library: \n" + err.what()); } catch (...) { - throw std::runtime_error( + throw torch::deploy::MultipyEmbeddedException( std::string(file_) + ":" + std::to_string(line_) + ": Unknown Exception Caught inside torch::deploy embedded library"); }