Catch all standard exceptions in C API. (#6220)
* `std::bad_alloc` is not guaranteed to be caught.
This commit is contained in:
parent
2241563f23
commit
70c2039748
@ -16,9 +16,17 @@
|
|||||||
#else // LOG_CAPI_INVOCATION
|
#else // LOG_CAPI_INVOCATION
|
||||||
#define API_BEGIN() try {
|
#define API_BEGIN() try {
|
||||||
#endif // LOG_CAPI_INVOCATION
|
#endif // LOG_CAPI_INVOCATION
|
||||||
|
|
||||||
/*! \brief every function starts with API_BEGIN();
|
/*! \brief every function starts with API_BEGIN();
|
||||||
and finishes with API_END() or API_END_HANDLE_ERROR */
|
and finishes with API_END() or API_END_HANDLE_ERROR */
|
||||||
#define API_END() } catch(dmlc::Error &_except_) { return XGBAPIHandleException(_except_); } return 0; // NOLINT(*)
|
#define API_END() \
|
||||||
|
} catch (dmlc::Error & _except_) { \
|
||||||
|
return XGBAPIHandleException(_except_); \
|
||||||
|
} catch (std::exception const &_except_) { \
|
||||||
|
return XGBAPIHandleException(dmlc::Error(_except_.what())); \
|
||||||
|
} \
|
||||||
|
return 0; // NOLINT(*)
|
||||||
|
|
||||||
#define CHECK_HANDLE() if (handle == nullptr) \
|
#define CHECK_HANDLE() if (handle == nullptr) \
|
||||||
LOG(FATAL) << "DMatrix/Booster has not been intialized or has already been disposed.";
|
LOG(FATAL) << "DMatrix/Booster has not been intialized or has already been disposed.";
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
@ -10,6 +10,8 @@
|
|||||||
#include "../helpers.h"
|
#include "../helpers.h"
|
||||||
#include "../../../src/common/io.h"
|
#include "../../../src/common/io.h"
|
||||||
|
|
||||||
|
#include "../../../src/c_api/c_api_error.h"
|
||||||
|
|
||||||
TEST(CAPI, XGDMatrixCreateFromMatDT) {
|
TEST(CAPI, XGDMatrixCreateFromMatDT) {
|
||||||
std::vector<int> col0 = {0, -1, 3};
|
std::vector<int> col0 = {0, -1, 3};
|
||||||
std::vector<float> col1 = {-4.0f, 2.0f, 0.0f};
|
std::vector<float> col1 = {-4.0f, 2.0f, 0.0f};
|
||||||
@ -196,4 +198,18 @@ TEST(CAPI, DMatrixSetFeatureName) {
|
|||||||
|
|
||||||
XGDMatrixFree(handle);
|
XGDMatrixFree(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int TestExceptionCatching() {
|
||||||
|
API_BEGIN();
|
||||||
|
throw std::bad_alloc();
|
||||||
|
API_END();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(CAPI, Exception) {
|
||||||
|
ASSERT_NO_THROW({TestExceptionCatching();});
|
||||||
|
ASSERT_EQ(TestExceptionCatching(), -1);
|
||||||
|
auto error = XGBGetLastError();
|
||||||
|
// Not null
|
||||||
|
ASSERT_TRUE(error);
|
||||||
|
}
|
||||||
} // namespace xgboost
|
} // namespace xgboost
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user