Interpreting error from thread join (Box<dyn Any + Send>)

I'm getting an error while joining a thread on android, and would like to read the error. The error is of type Box<dyn Any + Send>

Spawning a thread using vanilla std::thread::spawn( .. ).join(), blocking the calling-thread to ensure the result completes

An error from .join() means that the child thread panicked. Before returning the error, the child thread will also call the panic handler, which by default prints the panic message to stderr.

If you don't have access to stderr because of where your program is running, you could use set_hook to install a panic handler that logs its information elsewhere instead.

Or the parent thread could cast the Box<dyn Any> to a string like this:

if let Err(err) = std::thread::spawn(...).join() {
    let msg = match err.downcast_ref::<&'static str>() {
        Some(s) => *s,
        None => match err.downcast_ref::<String>() {
            Some(s) => &s[..],
            None => "Sorry, unknown payload type",
        },
    };
    // ... log the message here ...
}
4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.