Skip to content

Commit 741b1ad

Browse files
committed
Update mrcal_jni.cpp
1 parent fdd4f2b commit 741b1ad

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/mrcal_jni.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <stdexcept>
2424
#include <vector>
2525

26+
#include <exception>
27+
2628
#include "mrcal_wrapper.h"
2729

2830
// JClass helper from wpilib
@@ -101,6 +103,17 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
101103

102104
} // extern "C"
103105

106+
static std::string what(const std::exception_ptr &eptr = std::current_exception())
107+
{
108+
if (!eptr) { throw std::bad_exception(); }
109+
110+
try { std::rethrow_exception(eptr); }
111+
catch (const std::exception &e) { return e.what() ; }
112+
catch (const std::string &e) { return e ; }
113+
catch (const char *e) { return e ; }
114+
catch (...) { return "who knows"; }
115+
}
116+
104117
/*
105118
* Class: org_photonvision_mrcal_MrCalJNI_mrcal_1calibrate
106119
* Method: 1camera
@@ -112,6 +125,9 @@ Java_org_photonvision_mrcal_MrCalJNI_mrcal_1calibrate_1camera
112125
jint boardHeight, jdouble boardSpacing, jint imageWidth, jint imageHeight,
113126
jdouble focalLenGuessMM)
114127
{
128+
try {
129+
130+
115131
// Pull out arrays. We rely on data being packed and aligned to make this
116132
// work! Observations should be [x, y, level]
117133
std::span<mrcal_point3_t> observations{
@@ -197,6 +213,14 @@ Java_org_photonvision_mrcal_MrCalJNI_mrcal_1calibrate_1camera
197213
rms_err, residuals, warp_x, warp_y, Noutliers);
198214

199215
return ret;
216+
217+
} catch (...) {
218+
std::cerr << "Calibration exception: " << what() << std::endl;
219+
220+
static char buff[512];
221+
strcpy(buff, what().c_str());
222+
env->ThrowNew(env->FindClass("java/lang/Exception"), buff);
223+
}
200224
}
201225

202226
/*

0 commit comments

Comments
 (0)