1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CORE_UTILS_MS_EXCEPTION_H_ 18 #define MINDSPORE_CORE_UTILS_MS_EXCEPTION_H_ 19 #include <exception> 20 #include <set> 21 #include <mutex> 22 #include <string> 23 #include "utils/ms_utils.h" 24 #include "utils/log_adapter.h" 25 #include "mindapi/base/macros.h" 26 namespace mindspore { 27 class ExceptionListener { 28 public: 29 virtual void OnException() = 0; 30 virtual ~ExceptionListener() = default; 31 }; 32 33 class MS_CORE_API MsException { 34 public: 35 static MsException &Instance(); 36 SetException()37 void SetException() { 38 exception_ptr_ = std::current_exception(); 39 if (exception_ptr_ != nullptr && listener_ != nullptr) { 40 auto listener = listener_; 41 listener_ = nullptr; 42 listener->OnException(); 43 } 44 } 45 CheckException()46 void CheckException() { 47 if (exception_ptr_ != nullptr) { 48 auto exception_ptr = exception_ptr_; 49 exception_ptr_ = nullptr; 50 MS_LOG(DEBUG) << "Find exception and rethrow"; 51 std::rethrow_exception(exception_ptr); 52 } 53 } 54 SetExceptionListener(ExceptionListener * listener)55 void SetExceptionListener(ExceptionListener *listener) { listener_ = listener; } 56 57 private: 58 MsException() = default; ~MsException()59 ~MsException() { listener_ = nullptr; } DISABLE_COPY_AND_ASSIGN(MsException)60 DISABLE_COPY_AND_ASSIGN(MsException) 61 ExceptionListener *listener_{nullptr}; 62 std::exception_ptr exception_ptr_{nullptr}; 63 }; 64 65 class MS_CORE_API StaticAnalysisException { 66 public: 67 static StaticAnalysisException &Instance(); 68 ClearException()69 void ClearException() { 70 std::lock_guard<std::mutex> lock(lock_); 71 msg_ = ""; 72 exception_ptr_ = nullptr; 73 } 74 HasException()75 bool HasException() { 76 std::lock_guard<std::mutex> lock(lock_); 77 return exception_ptr_ != nullptr; 78 } 79 SetException()80 void SetException() { 81 std::lock_guard<std::mutex> lock(lock_); 82 if (exception_ptr_ != nullptr) { 83 return; 84 } 85 exception_ptr_ = std::current_exception(); 86 } AppendMsg(const std::string & msg)87 void AppendMsg(const std::string &msg) { 88 std::lock_guard<std::mutex> lock(lock_); 89 msg_ += msg; 90 } msg()91 std::string msg() { 92 std::lock_guard<std::mutex> lock(lock_); 93 return msg_; 94 } 95 SetAndRethrowException()96 void SetAndRethrowException() { 97 std::lock_guard<std::mutex> lock(lock_); 98 SetException(); 99 std::rethrow_exception(std::current_exception()); 100 } 101 CheckException()102 void CheckException() { 103 std::lock_guard<std::mutex> lock(lock_); 104 if (exception_ptr_ != nullptr) { 105 auto tmp_exception_ptr = exception_ptr_; 106 std::rethrow_exception(tmp_exception_ptr); 107 } 108 } 109 110 private: 111 StaticAnalysisException() = default; 112 ~StaticAnalysisException() = default; DISABLE_COPY_AND_ASSIGN(StaticAnalysisException)113 DISABLE_COPY_AND_ASSIGN(StaticAnalysisException) 114 115 std::exception_ptr exception_ptr_{nullptr}; 116 std::string msg_; 117 std::mutex lock_; 118 }; 119 } // namespace mindspore 120 121 #endif // MINDSPORE_CORE_UTILS_MS_EXCEPTION_H_ 122