exception.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 _TARS_EXCEPTION_H_
  17. #define _TARS_EXCEPTION_H_
  18. #include <exception>
  19. #include <memory>
  20. #include <cstring>
  21. #include <string>
  22. namespace tars {
  23. class ExceptionBase : public std::exception {
  24. public:
  25. ExceptionBase() {}
  26. ~ExceptionBase() throw() override {}
  27. const char* what() const throw() override
  28. {
  29. return "exception_base";
  30. }
  31. virtual ExceptionBase* clone() const
  32. {
  33. return new ExceptionBase(*this);
  34. }
  35. virtual int code() const { return -1; }
  36. virtual void rethrow() const { throw *this; }
  37. };
  38. class Exception : public ExceptionBase {
  39. public:
  40. Exception(const std::string& info, const int code = 0)
  41. : m_info(info)
  42. , m_code(code)
  43. {
  44. if (code != 0)
  45. m_info = m_info + ":" + std::strerror(code);
  46. }
  47. ~Exception() throw() override
  48. {
  49. }
  50. const char* what() const throw() override
  51. {
  52. return m_info.c_str();
  53. }
  54. ExceptionBase* clone() const override
  55. {
  56. return new Exception(*this);
  57. }
  58. int code() const override { return m_code; }
  59. void rethrow() const override { throw *this; }
  60. protected:
  61. std::string m_info;
  62. int m_code;
  63. };
  64. using ExceptionPtr = std::shared_ptr<ExceptionBase>;
  65. inline ExceptionPtr currentException()
  66. {
  67. try {
  68. throw;
  69. } catch (Exception& e) {
  70. return ExceptionPtr(e.clone());
  71. } catch (ExceptionBase& e) {
  72. return ExceptionPtr(e.clone());
  73. } catch (std::exception& e) {
  74. return ExceptionPtr(new Exception(std::string(e.what())));
  75. } catch (...) {
  76. }
  77. return ExceptionPtr(new Exception("unknow_exception"));
  78. }
  79. inline ExceptionPtr copyException(const std::string& e, int err)
  80. {
  81. try {
  82. throw Exception(e, err);
  83. } catch (...) {
  84. return currentException();
  85. }
  86. }
  87. } // end namespace tars
  88. #endif