1 unstable release

Uses new Rust 2024

new 0.1.0 May 2, 2025

#861 in Rust patterns

Custom license

5KB

trycatch

Throw and catch exceptions in rust.

(Please don't use this crate.)

use trycatch::{throw, try_catch};

fn divide(x: u32, y: u32) -> u32 {
    if y == 0 {
        throw!("y cannot be zero");
    }
    return x / y;
}

fn main() {
    try_catch! {
        try {
            divide(10, 0);
        } catch (e) {
            println!("Error: {e}");
        }
    };
}
Macro expansion
#![feature(prelude_import)]
#![allow(clippy::needless_return)]
#[prelude_import]
use std::prelude::rust_2024::*;
#[macro_use]
extern crate std;
use trycatch::{throw, try_catch};
fn divide(x: u32, y: u32) -> u32 {
    if y == 0 {
        {
            ::core::panicking::panic_fmt(format_args!("y cannot be zero"));
        };
    }
    return x / y;
}
fn main() {
    let original_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(|_| {}));
    match std::panic::catch_unwind(|| {
        divide(10, 0);
    }) {
        Err(e) => {
            if let Some(&e) = e.downcast_ref::<&str>() {
                {
                    {
                        ::std::io::_print(format_args!("Error: {0}\n", e));
                    };
                }
            } else {
                ::core::panicking::panic("internal error: entered unreachable code");
            }
        }
        Ok(v) => v,
    }
    std::panic::set_hook(original_hook);
}

No runtime deps