What are the differences between panic in Rust and exception in C++?

I haven't worked with C++ exceptions a lot, but I know exceptions in general. I think the difference between exceptions in general and Rust's panics might be:

  • It's not guaranteed that a panic can be caught (you can demand an abort on panic during compilation).
  • Thus panics should not be used for recoverable errors in general.
  • When catching a panic, you get a dynamically typed object implementing the Any trait, see std::thread::Result, which is returned by std::panic::catch_unwind. This may be troublesome to make use of.

I think catching panics is mostly for making a multithreaded process not abort but keep functioning in case of serious, non-recoverable errors.

Even though Lua and Rust are fundamentally different languages, I feel like Lua's errors (raised with the error function) are similar to Rust's panics. Respectively, returning a nil as first value and an error_value as second return value in Lua corresponds to returning std::result::Result::Err(error_value) in Rust.

Lua, like Rust, doesn't have exceptions.

1 Like