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(
28 : HcclResult errorCode, const std::string& errString, const char* fileName, s32 lineNum,
29 : const char* functionName);
30 : };
31 :
32 : // HCCL异常码类,用于识别特定HCCL错误码
33 : class HcclException : public std::exception {
34 : public:
35 0 : HcclException(HcclResult code, const std::string& msg) : code_(code), msg_(msg) {}
36 :
37 0 : HcclResult code() const { return code_; }
38 :
39 0 : const char* what() const noexcept override { return msg_.c_str(); }
40 :
41 : private:
42 : HcclResult code_;
43 : std::string msg_;
44 : };
45 :
46 : // 宏定义,用于包装 C 接口函数的异常处理
47 : #define EXCEPTION_HANDLE_BEGIN try {
48 : #define EXCEPTION_HANDLE_END_INFO(func_name) \
49 : } \
50 : catch (...) { return hccl::ExceptionHandler::HandleException(func_name); }
51 :
52 : #define EXCEPTION_HANDLE_END EXCEPTION_HANDLE_END_INFO(__func__)
53 :
54 : #define EXCEPTION_THROW_IF_ERR(call, errString) \
55 : do { \
56 : HcclResult ret = call; \
57 : hccl::ExceptionHandler::ThrowIfErrorCode(ret, errString, __FILE__, __LINE__, __func__); \
58 : } while (0)
59 :
60 : #define EXCEPTION_THROW_IF_COND_ERR(condition, errString) \
61 : do { \
62 : if (condition) { \
63 : HcclResult ret = HCCL_E_INTERNAL; \
64 : hccl::ExceptionHandler::ThrowIfErrorCode(ret, errString, __FILE__, __LINE__, __func__); \
65 : } \
66 : } while (0)
67 :
68 : } // namespace hccl
69 :
70 : #endif
|