#ffi #jni #java


A helper macro for safe Java-Rust interop that "catches" Rust panics and rethrows them as Java exceptions

1 stable release

1.0.0 Jul 25, 2022

#1115 in Rust patterns

Download history 159/week @ 2022-11-26 394/week @ 2022-12-03 265/week @ 2022-12-10 309/week @ 2022-12-17 236/week @ 2022-12-24 104/week @ 2022-12-31 391/week @ 2023-01-07 342/week @ 2023-01-14 1194/week @ 2023-01-21 1129/week @ 2023-01-28 1166/week @ 2023-02-04 594/week @ 2023-02-11 1056/week @ 2023-02-18 610/week @ 2023-02-25 747/week @ 2023-03-04 756/week @ 2023-03-11

3,172 downloads per month


119 lines

⚾ #[catch_panic]

Crates.io Documentation License

A helper macro for safe Java-Rust interop that "catches" Rust panics and rethrows them as Java exceptions.

Getting Started

Add catch_panic as a dependency to your Cargo.toml:

catch_panic = "1.0.0"


Attach #[catch_panic] to a JNI callback to have panics converted into RuntimeExceptions:

use jni::JNIEnv;
use catch_panic::catch_panic;

pub extern "C" fn Java_com_example_Example_panic(_env: JNIEnv) {
    panic!("everything is not fine");

See the docs for macro options and more information.


This crate is dual-licensed under either:

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

The code in this crate is a derivative of code from HermitSocialClub/HermitRepo, specifically the files ProjectTomato/tomato_macros/src/lib.rs and ProjectTomato/tomato/src/util.rs. All authors of these two files have agreed to relicense the original code under the above license.


~103K SLoC