Skip to content

Spawned task panics instead of returning error in JoinHandle #5624

Answered by Darksonn
kevin-june asked this question in Q&A
Discussion options

You must be logged in to vote

The panic message is printed even if you catch it. Try this:

use tokio::task;
use std::io;
use std::panic;

#[tokio::main]
async fn main() -> io::Result<()> {
    let join_handle: task::JoinHandle<Result<i32, io::Error>> = tokio::spawn(async {
        panic!("boom");
    });

    let err = join_handle.await.unwrap_err();
    assert!(err.is_panic());

    println!("at the end");

    Ok(())
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kevin-june
Comment options

Answer selected by kevin-june
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants