#error-handling #exceptions


A library for error-handling syntax in Rust

2 stable releases

1.0.1 Jun 29, 2023

#433 in Rust patterns

Download history 11/week @ 2023-08-04 23/week @ 2023-08-11 204/week @ 2023-08-18 88/week @ 2023-08-25 56/week @ 2023-09-01 71/week @ 2023-09-08 211/week @ 2023-09-15 160/week @ 2023-09-22 183/week @ 2023-09-29 189/week @ 2023-10-06 175/week @ 2023-10-13 116/week @ 2023-10-20 177/week @ 2023-10-27 167/week @ 2023-11-03 228/week @ 2023-11-10 177/week @ 2023-11-17

753 downloads per month
Used in cargo-dl


99 lines

version-badge license-badge rust-version-badge

Adding support for "throwing functions" to Rust through procedural macros. Functions marked with the throws attribute return Result, but the "Ok" path is used by default and you don't need to wrap ok return values in Ok. To throw errors, use ? or the throws macro.

A fork of Der Fehler updating dependencies and fixing issues while (by my understanding) boats is unable to contribute to open source.

The #[throws] attribute

The throws attribute modifies a function or method to make it return a Result. It takes an optional typename as an argument to the attribute which will be the error type of this function; if no typename is supplied, it uses the default error type for this crate.

Within the function body, returns (including the implicit final return) are automatically "Ok-wrapped." To raise errors, use ? or the throws! macro.

For example, these two functions are equivalent:

fn foo(x: bool) -> i32 {
    if x {
    } else {

fn bar(x: bool) -> Result<i32, i32> {
    if x {
    } else {

In functions that return Option

The attribute can be used to make a function that returns an Option using the as Option syntax, demonstrated below:

// This function returns `Option<i32>`
#[throws(as Option)]
fn foo(x: bool) -> i32 {
    if x {
    } else {

The throw! macro

throw! is a macro which is equivalent to the Err($e)? pattern. It takes an error type and "throws" it.

One important aspect of the throw! macro is that it allows you to return errors inside of functions marked with throws. You cannot just return errors from these functions, you need to use this macro.

Rust Version Policy

This crate only supports the current stable version of Rust, patch releases may use new features at any time.


Licensed under either of

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.


~18K SLoC