use std::any::Any;
use std::panic::{catch_unwind, UnwindSafe};
use jni::JNIEnv;
#[doc(hidden)]
#[inline]
pub fn __catch_panic<F, R, H>(env: JNIEnv, default: R, handler: H, f: F) -> R
where
F: FnOnce() -> R + UnwindSafe,
H: FnOnce(JNIEnv, Box<dyn Any + Send + 'static>),
{
match catch_unwind(f) {
Ok(result) => result,
Err(err) => {
handler(env, err);
default
}
}
}
pub fn default_handler(env: JNIEnv, err: Box<dyn Any + Send + 'static>) {
let msg = match err.downcast_ref::<&'static str>() {
Some(s) => *s,
None => match err.downcast_ref::<String>() {
Some(s) => &s[..],
None => "Box<dyn Any>",
},
};
env.throw_new("java/lang/RuntimeException", msg).unwrap();
}