exception.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of NVIDIA CORPORATION nor the names of its
  12. * contributors may be used to endorse or promote products derived
  13. * from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /* CUda UTility Library */
  28. #ifndef COMMON_EXCEPTION_H_
  29. #define COMMON_EXCEPTION_H_
  30. // includes, system
  31. #include <stdlib.h>
  32. #include <exception>
  33. #include <iostream>
  34. #include <stdexcept>
  35. #include <string>
  36. //! Exception wrapper.
  37. //! @param Std_Exception Exception out of namespace std for easy typing.
  38. template <class Std_Exception>
  39. class Exception : public Std_Exception {
  40. public:
  41. //! @brief Static construction interface
  42. //! @return Alwayss throws ( Located_Exception<Exception>)
  43. //! @param file file in which the Exception occurs
  44. //! @param line line in which the Exception occurs
  45. //! @param detailed details on the code fragment causing the Exception
  46. static void throw_it(const char *file, const int line,
  47. const char *detailed = "-");
  48. //! Static construction interface
  49. //! @return Alwayss throws ( Located_Exception<Exception>)
  50. //! @param file file in which the Exception occurs
  51. //! @param line line in which the Exception occurs
  52. //! @param detailed details on the code fragment causing the Exception
  53. static void throw_it(const char *file, const int line,
  54. const std::string &detailed);
  55. //! Destructor
  56. virtual ~Exception() throw();
  57. private:
  58. //! Constructor, default (private)
  59. Exception();
  60. //! Constructor, standard
  61. //! @param str string returned by what()
  62. explicit Exception(const std::string &str);
  63. };
  64. ////////////////////////////////////////////////////////////////////////////////
  65. //! Exception handler function for arbitrary exceptions
  66. //! @param ex exception to handle
  67. ////////////////////////////////////////////////////////////////////////////////
  68. template <class Exception_Typ>
  69. inline void handleException(const Exception_Typ &ex) {
  70. std::cerr << ex.what() << std::endl;
  71. exit(EXIT_FAILURE);
  72. }
  73. //! Convenience macros
  74. //! Exception caused by dynamic program behavior, e.g. file does not exist
  75. #define RUNTIME_EXCEPTION(msg) \
  76. Exception<std::runtime_error>::throw_it(__FILE__, __LINE__, msg)
  77. //! Logic exception in program, e.g. an assert failed
  78. #define LOGIC_EXCEPTION(msg) \
  79. Exception<std::logic_error>::throw_it(__FILE__, __LINE__, msg)
  80. //! Out of range exception
  81. #define RANGE_EXCEPTION(msg) \
  82. Exception<std::range_error>::throw_it(__FILE__, __LINE__, msg)
  83. ////////////////////////////////////////////////////////////////////////////////
  84. //! Implementation
  85. // includes, system
  86. #include <sstream>
  87. ////////////////////////////////////////////////////////////////////////////////
  88. //! Static construction interface.
  89. //! @param Exception causing code fragment (file and line) and detailed infos.
  90. ////////////////////////////////////////////////////////////////////////////////
  91. /*static*/ template <class Std_Exception>
  92. void Exception<Std_Exception>::throw_it(const char *file, const int line,
  93. const char *detailed) {
  94. std::stringstream s;
  95. // Quiet heavy-weight but exceptions are not for
  96. // performance / release versions
  97. s << "Exception in file '" << file << "' in line " << line << "\n"
  98. << "Detailed description: " << detailed << "\n";
  99. throw Exception(s.str());
  100. }
  101. ////////////////////////////////////////////////////////////////////////////////
  102. //! Static construction interface.
  103. //! @param Exception causing code fragment (file and line) and detailed infos.
  104. ////////////////////////////////////////////////////////////////////////////////
  105. /*static*/ template <class Std_Exception>
  106. void Exception<Std_Exception>::throw_it(const char *file, const int line,
  107. const std::string &msg) {
  108. throw_it(file, line, msg.c_str());
  109. }
  110. ////////////////////////////////////////////////////////////////////////////////
  111. //! Constructor, default (private).
  112. ////////////////////////////////////////////////////////////////////////////////
  113. template <class Std_Exception>
  114. Exception<Std_Exception>::Exception() : Std_Exception("Unknown Exception.\n") {}
  115. ////////////////////////////////////////////////////////////////////////////////
  116. //! Constructor, standard (private).
  117. //! String returned by what().
  118. ////////////////////////////////////////////////////////////////////////////////
  119. template <class Std_Exception>
  120. Exception<Std_Exception>::Exception(const std::string &s) : Std_Exception(s) {}
  121. ////////////////////////////////////////////////////////////////////////////////
  122. //! Destructor
  123. ////////////////////////////////////////////////////////////////////////////////
  124. template <class Std_Exception>
  125. Exception<Std_Exception>::~Exception() throw() {}
  126. // functions, exported
  127. #endif // COMMON_EXCEPTION_H_