Is there any way to get the line number of the code which caused the task panic #6636
Unanswered
tongacuity
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I think you could use custom panic handler by using set_hook. use std::panic;
#[tokio::main]
async fn main() {
panic::set_hook(Box::new(|panic_info| {
if let Some(location) = panic_info.location() {
println!("panic occurred in file '{:?}'", location);
} else {
println!("panic occurred but can't get location information...");
}
}));
let handle = tokio::spawn(async {
panic!("Panicking in the task");
});
let result = handle.await;
match result {
Ok(_) => {}
Err(e) => {
if e.is_panic() {
println!("Task panicked with info: {:?}", e);
}
}
}
} EDIT: It seems that you got an answer in discord as well. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi folks,
I am a beginner rust and tokio user so I hope this is the right place to ask my question.
I am trying to get more information from the task when it's panicking.
What I really want to know the line number which caused the panic but I don't know how to get it from the tokio::task::JoinError
With below code, my println! only prints out
Task panicked with info: Any { .. }
Is there any way to get the exactly line number which caused panic and print it out using println!?
Beta Was this translation helpful? Give feedback.
All reactions