HException.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, Herror err = H_MSG_OK);
  35. static void Throw(const char *proc_name, Herror err);
  36. // Conversion and copying
  37. static void GetExceptionData(const HTuple &exception,
  38. const HTuple &name, HTuple *value);
  39. void ToHTuple(HTuple *exception) const;
  40. HException& operator = (const HException &except);
  41. // Accessors
  42. const HString& ProcName() const {return mProcName;}
  43. const HString& ErrorMessage() const {return mErrorMessage;}
  44. Herror ErrorCode() const {return mErrorCode;}
  45. HDEPRECATED(const HString& ErrorText() const ,
  46. "deprecated, please use ErrorMessage instead.")
  47. {return mErrorMessage;}
  48. HDEPRECATED(Herror ErrorNumber() const ,
  49. "deprecated, please use ErrorCode instead.")
  50. {return mErrorCode;}
  51. const HTuple& UserData() const {return mUserData;}
  52. // Legacy
  53. #ifdef HCPP_LEGACY_EXCEPTION
  54. // Handler for user defined exception handling.
  55. // Used with InstallHHandler()
  56. typedef void (*Handler)(const HException &exception);
  57. void React(Hproc_handle proc_handle) const;
  58. static Handler InstallHHandler(Handler proc);
  59. // default exception handler
  60. void PrintException(void) const;
  61. static Handler handler; /* handler in use */
  62. #endif
  63. protected:
  64. HString mProcName; // Name of procedure/operator
  65. HString mErrorMessage; // Error message
  66. Herror mErrorCode; // Error code
  67. HTuple mUserData; // User defined error data
  68. private:
  69. void InitException(const HString& proc_name, const HString& msg,
  70. Herror err, const HTuple& user_data);
  71. };
  72. // Exception that is raised if operator call in HALCON library fails
  73. class LIntExport HOperatorException : public HException
  74. {
  75. public:
  76. // Failure during operator call
  77. HOperatorException(Hproc_handle proc, Herror err);
  78. // Precall failure before procedure handle is available
  79. HOperatorException(HINT proc_index, Herror err);
  80. // Failure during operator-like call, e.g. emulated dev_* operators
  81. HOperatorException(const char* name, Herror err);
  82. HOperatorException(const HOperatorException &except);
  83. static void Throw(Hproc_handle proc, Herror err);
  84. static void Throw(HINT proc_index, Herror err);
  85. static void Throw(const char* name, Herror err);
  86. void ThrowInstance() const;
  87. // Extended Error Info
  88. HString ExtendedErrorMessage() const;
  89. INT4_8 ExtendedErrorCode() const;
  90. };
  91. // Exception that is raised from user code (code export)
  92. class LIntExport HUserException : public HException
  93. {
  94. public:
  95. HUserException(Herror err, const char *msg);
  96. HUserException(const HUserException &except);
  97. HUserException(const HTuple &tuple);
  98. };
  99. // Exception that is raised for tuple-related errors
  100. class LIntExport HTupleAccessException: public HException
  101. {
  102. public:
  103. HTupleAccessException(void) : HException("","Illegal operation on tuple") {}
  104. HTupleAccessException(const char *m) : HException("",m) {}
  105. HTupleAccessException(Herror err) : HException("",err) {}
  106. static void Throw(void);
  107. static void Throw(const char* m);
  108. static void Throw(Herror err);
  109. void ThrowInstance() const;
  110. ~HTupleAccessException();
  111. };
  112. // Exception that is raised for vector-related errors
  113. class LIntExport HVectorAccessException: public HException
  114. {
  115. public:
  116. HVectorAccessException(void) : HException("",
  117. "Illegal operation on vector") {}
  118. HVectorAccessException(const char *m) : HException("",m) {}
  119. HVectorAccessException(Herror err) : HException("",err) {}
  120. HVectorAccessException(const char *m, Herror err) : HException("",m,err) {}
  121. static void Throw(void);
  122. static void Throw(const char* m);
  123. static void Throw(Herror err);
  124. void ThrowInstance() const;
  125. ~HVectorAccessException();
  126. };
  127. }
  128. #endif