tc_ex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_EX_H
  17. #define __TC_EX_H
  18. #include <stdexcept>
  19. #include <string>
  20. using namespace std;
  21. namespace tars
  22. {
  23. /////////////////////////////////////////////////
  24. /**
  25. * @file tc_ex.h
  26. * @brief 异常类
  27. */
  28. /////////////////////////////////////////////////
  29. /**
  30. * @brief 异常类.
  31. */
  32. class TC_Exception : public exception
  33. {
  34. public:
  35. /**
  36. * @brief 构造函数,提供了一个可以传入errno的构造函数
  37. * @param err, 是否附带系统错误信息(linux版本, 用errno获取错误码, windows版本, 用GetLastError()获取错误)
  38. *
  39. * @param buffer 异常的告警信息
  40. */
  41. explicit TC_Exception(const string &buffer);
  42. /**
  43. * @brief 构造函数,提供了一个可以传入errno的构造函数,
  44. *
  45. * 异常抛出时直接获取的错误信息
  46. *
  47. * @param buffer 异常的告警信息
  48. * @param errno 传入:errno, windows版本 传入:GetLastError()
  49. */
  50. TC_Exception(const string &buffer, int err);
  51. /**
  52. * @brief 析够数函
  53. */
  54. virtual ~TC_Exception() throw();
  55. /**
  56. * @brief 错误信息.
  57. *
  58. * @return const char*
  59. */
  60. virtual const char* what() const throw();
  61. /**
  62. * @brief 获取错误码
  63. *
  64. * @return 成功获取返回0
  65. */
  66. int getErrCode() { return _code; }
  67. /**
  68. * @brief 获取错误字符串(linux是errno, windows是GetLastError())
  69. *
  70. * @return 成功获取返回0
  71. */
  72. static string parseError(int err);
  73. /**
  74. * @brief 获取系统错误码(linux是errno, windows是GetLastError)
  75. *
  76. * @return 成功获取返回0
  77. */
  78. static int getSystemCode();
  79. private:
  80. void getBacktrace();
  81. private:
  82. /**
  83. * 错误码
  84. */
  85. int _code;
  86. /**
  87. * 异常的相关信息
  88. */
  89. string _buffer;
  90. };
  91. //为了避免windows平台GetLastError()获取不对的问题, 因为抛异常, throw TC_Exception("xxxx", TC_Exception::getSystemCode())时
  92. //回调用系统函数分配内存, 导致错误码被改写, 因此专门定义宏来抛出异常
  93. //先获取到错误码, 再throw
  94. #define THROW_EXCEPTION_SYSCODE(EX_CLASS, buffer) \
  95. { \
  96. int ret = TC_Exception::getSystemCode(); \
  97. throw EX_CLASS(buffer, ret); \
  98. }
  99. }
  100. #endif