4 ms·
>I like exceptions. You can't get away from them, I know Haskell tried, Rust tried, they still have exceptions (perhaps they call them by a different name thoug
by byby 3y ago
>I like exceptions. You can't get away from them, I know Haskell tried, Rust tried, they still have exceptions (perhaps they call them by a different name though)
Rust has no exceptions in any sense of the word. Exceptions are ways to crash the program in a controlled manner with the ability for these crashes to be caught midway before the program crashes and handled correctly.
In rust you can only crash it explicitly using the panic! Macro. No handling.
The different name you are looking for is "errors". Rust has errors as do all programs. Some programs have exceptions to handle those errors. Rust does not.
If you mean "runtime crashes" then elm is the only language that is popular that uses has no runtime crashes.
- Georgelemental 3y ago> Exceptions are ways to crash the program in a controlled manner with the ability for these crashes to be caught midway before the program crashes and handled correctly. > In rust you can only crash it explicitly using the panic! Macro. No handling. You can catch Rust panics with `catch_unwind()` if you compiled your program with `-Cpanic=unwind` (default on most platforms). Under the hood it uses stack unwinding, just like C++ and co's exceptions. But Rust doesn't consider this idiomatic error handling, it's only used in a few special situations (test frameworks, web servers where one buggy session shouldn't take down the process...).
- steveklabnik 3y agoI don’t disagree with you that just because rust has a feature similar to exceptions, that it has exceptions, but https://doc.rust-lang.org/std/panic/fn.catch_unwind.html https://doc.rust-lang.org/std/panic/fn.catch_unwind.html does exist.
- Buttons840 3y agoYou absolutely can catch and handle a panic in Rust [0]. Panics are just exceptions by a different name. Although, I do agree that the Rust ecosystem tries hard to avoid using panics, which is good. [0] Example of catching, handling, and "swallowing" a panic in Rust (GTP4 wrote this): https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9396dd4cd1714e11e77931faa0ed7638 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- awestroke 3y agoThis is very uncommon to do and cannot be compared to exceptions
- byby 3y agoLearned something new. I feel though it's better if most programmers didn't know about this. It looks like cursed knowledge.
- dmytrish 3y agoIt's not exactly cursed knowledge, you just have to have enough judgement to understand where it's appropriate. Trying to do magic and patch something on a panic and retry is cursed indeed. If it's about cleaning up resources of a thread and logging this somewhere, then there's nothing cursed about this.
- sriram_malhar 3y agoCatching a panic is _morally_ equivalent to catching a SIGSEGV. It is not meant for normal program error conditions and expected failures (e.g. "No such file").
- riwsky 3y agoI.e. only to be used in exceptional cases.