rs-basic/members/panic-calm/src/main.rs

31 lines
692 B
Rust
Raw Normal View History

2024-01-12 15:42:11 +01:00
use std::panic;
2024-01-12 15:39:54 +01:00
fn inner() {
let mut counter = 0u8;
loop {
// will eventually panic when overflowing?
counter += 1;
print!("{counter}\t");
2024-01-12 15:42:11 +01:00
if counter % 8 == 0 {
println!()
}
2024-01-12 15:39:54 +01:00
if counter == 255 {
// so panic will look fancier :)
println!()
}
}
}
fn main() {
// will not catch all panics, only ones that unwind
let panic = panic::catch_unwind(|| {
inner();
});
if panic.is_err() {
dbg!(&panic);
dbg!(&panic.as_ref().unwrap_err().type_id());
println!("recovered from a panic");
2024-01-12 15:42:11 +01:00
} else {
println!("no panic on the titanic")
2024-01-12 15:39:54 +01:00
}
}