HException.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*****************************************************************************
  2. * HException.h
  3. *****************************************************************************
  4. *
  5. * Project: HALCON/C++
  6. * Description: Error handling
  7. *
  8. * (c) 2010-2020 by MVTec Software GmbH
  9. * www.mvtec.com
  10. *
  11. *****************************************************************************
  12. *
  13. *
  14. *****************************************************************************/
  15. #ifndef HCPP_EXCEPTION_H
  16. #define HCPP_EXCEPTION_H
  17. #include "halconcpp/HString.h"
  18. #include "halconcpp/HTuple.h"
  19. namespace HalconCpp
  20. {
  21. // Base class for all exception that is raised for generic
  22. // errors within the HALCON/C++ interface
  23. class LIntExport HException
  24. {
  25. public:
  26. // Constructors / Destructor
  27. HException(const char* proc_name, Herror err);
  28. HException(const char* proc_name, const char* msg, Herror err = H_MSG_OK);
  29. HException(const HException& except);
  30. HException(const HTuple& tuple);
  31. virtual ~HException();
  32. // Raising exceptions (synchronization-aware)
  33. virtual void ThrowInstance() const;
  34. static void Throw(const char* proc_name, const char* msg,
  35. Herror err = H_MSG_OK);
  36. static void Throw(const char* proc_name, Herror err);
  37. // Conversion and copying
  38. static void GetExceptionData(const HTuple& exception, const HTuple& name,
  39. HTuple* value);
  40. void ToHTuple(HTuple* exception) const;
  41. HException& operator=(const HException& except);
  42. // Accessors
  43. const HString& ProcName() const
  44. {
  45. return mProcName;
  46. }
  47. const HString& ErrorMessage() const
  48. {
  49. return mErrorMessage;
  50. }
  51. Herror ErrorCode() const
  52. {
  53. return mErrorCode;
  54. }
  55. HDEPRECATED(const HString& ErrorText() const,
  56. "deprecated, please use ErrorMessage instead.")
  57. {
  58. return mErrorMessage;
  59. }
  60. HDEPRECATED(Herror ErrorNumber() const,
  61. "deprecated, please use ErrorCode instead.")
  62. {
  63. return mErrorCode;
  64. }
  65. const HTuple& UserData() const
  66. {
  67. return mUserData;
  68. }
  69. // Legacy
  70. #ifdef HCPP_LEGACY_EXCEPTION
  71. // Handler for user defined exception handling.
  72. // Used with InstallHHandler()
  73. typedef void (*Handler)(const HException& exception);
  74. void React(Hproc_handle proc_handle) const;
  75. static Handler InstallHHandler(Handler proc);
  76. // default exception handler
  77. void PrintException(void) const;
  78. static Handler handler; /* handler in use */
  79. #endif
  80. protected:
  81. HString mProcName; // Name of procedure/operator
  82. HString mErrorMessage; // Error message
  83. Herror mErrorCode; // Error code
  84. HTuple mUserData; // User defined error data
  85. private:
  86. void InitException(const HString& proc_name, const HString& msg, Herror err,
  87. const HTuple& user_data);
  88. };
  89. // Exception that is raised if operator call in HALCON library fails
  90. class LIntExport HOperatorException : public HException
  91. {
  92. public:
  93. // Failure during operator call
  94. HOperatorException(Hproc_handle proc, Herror err);
  95. // Precall failure before procedure handle is available
  96. HOperatorException(int proc_index, Herror err);
  97. // Failure during operator-like call, e.g. emulated dev_* operators
  98. HOperatorException(const char* name, Herror err);
  99. HOperatorException(const HOperatorException& except);
  100. static void Throw(Hproc_handle proc, Herror err);
  101. static void Throw(int proc_index, Herror err);
  102. static void Throw(const char* name, Herror err);
  103. void ThrowInstance() const;
  104. // Extended Error Info
  105. HString ExtendedErrorMessage() const;
  106. INT4_8 ExtendedErrorCode() const;
  107. };
  108. // Exception that is raised from user code (code export)
  109. class LIntExport HUserException : public HException
  110. {
  111. public:
  112. HUserException(Herror err, const char* msg);
  113. HUserException(const HUserException& except);
  114. HUserException(const HTuple& tuple);
  115. };
  116. // Exception that is raised for tuple-related errors
  117. class LIntExport HTupleAccessException : public HException
  118. {
  119. public:
  120. HTupleAccessException(void) : HException("", "Illegal operation on tuple") {}
  121. HTupleAccessException(const char* m) : HException("", m) {}
  122. HTupleAccessException(Herror err) : HException("", err) {}
  123. static void Throw(void);
  124. static void Throw(const char* m);
  125. static void Throw(Herror err);
  126. void ThrowInstance() const;
  127. ~HTupleAccessException();
  128. };
  129. // Exception that is raised for vector-related errors
  130. class LIntExport HVectorAccessException : public HException
  131. {
  132. public:
  133. HVectorAccessException(void) : HException("", "Illegal operation on vector")
  134. {
  135. }
  136. HVectorAccessException(const char* m) : HException("", m) {}
  137. HVectorAccessException(Herror err) : HException("", err) {}
  138. HVectorAccessException(const char* m, Herror err) : HException("", m, err) {}
  139. static void Throw(void);
  140. static void Throw(const char* m);
  141. static void Throw(Herror err);
  142. void ThrowInstance() const;
  143. ~HVectorAccessException();
  144. };
  145. }
  146. #endif