Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef EXCEPTION_HANDLER_H
12 : #define EXCEPTION_HANDLER_H
13 :
14 : #include <stdexcept>
15 : #include <string>
16 : #include <hccl/hccl_types.h>
17 : #include <hccl/base.h>
18 : #include "log.h"
19 :
20 : namespace hccl {
21 :
22 : // 异常处理器类
23 : class ExceptionHandler {
24 : public:
25 : static HcclResult HandleException(const char* functionName);
26 :
27 : static void ThrowIfErrorCode(HcclResult errorCode, const std::string &errString, const char* fileName,
28 : s32 lineNum, const char* functionName);
29 : };
30 :
31 : // HCCL异常码类,用于识别特定HCCL错误码
32 : class HcclException : public std::exception {
33 : public:
34 0 : HcclException(HcclResult code, const std::string &msg)
35 0 : : code_(code), msg_(msg) {}
36 :
37 0 : HcclResult code() const { return code_; }
38 :
39 0 : const char* what() const noexcept override {
40 0 : return msg_.c_str();
41 : }
42 :
43 : private:
44 : HcclResult code_;
45 : std::string msg_;
46 : };
47 :
48 : // 宏定义,用于包装 C 接口函数的异常处理
49 : #define EXCEPTION_HANDLE_BEGIN try {
50 : #define EXCEPTION_HANDLE_END_INFO(func_name) } catch (...) { \
51 : return hccl::ExceptionHandler::HandleException(func_name); }
52 :
53 : #define EXCEPTION_HANDLE_END EXCEPTION_HANDLE_END_INFO(__func__)
54 :
55 : #define EXCEPTION_THROW_IF_ERR(call, errString) \
56 : do { \
57 : HcclResult ret = call; \
58 : hccl::ExceptionHandler::ThrowIfErrorCode(ret, errString, __FILE__, __LINE__, __func__); \
59 : } while (0)
60 :
61 : #define EXCEPTION_THROW_IF_COND_ERR(condition, errString) \
62 : do { \
63 : if (condition) { \
64 : HcclResult ret = HCCL_E_INTERNAL; \
65 : hccl::ExceptionHandler::ThrowIfErrorCode(ret, errString, __FILE__, __LINE__, __func__); \
66 : } \
67 : } while (0)
68 :
69 : }
70 :
71 : #endif
|