Hacker News new | past | comments | ask | show | jobs | submit login

They are not even close. In Python they are a control flow mechanism (ie StopIteration), in Rust they signal a program-terminating error.



Python is exceptionally exception happy.

Java has the checked/unchecked exception divide, where checked exceptions for expected errors that need to be handled specifically, and unchecked exceptions for unexpected errors, matching the use cases of Result and panic respectively in Rust. Unfortunately Java's checked exceptions are cumbersome and don't integrate well with interfaces or generics.

In C# the role of exceptions is not so clearly defined, largely due to the lack of a good alternative error handling mechanism. You have influential developers like Eric Lippert (then part of the C# compiler team) argue against using exceptions for expected errors https://ericlippert.com/2008/09/10/vexing-exceptions . Though I'd go further and would call the use of exceptions to signal IO errors an unfortunate design decision of the language/framework designers. My own experience as web developer is that exceptions will almost always be caught only at the request handler level and turn into an http error.

In C++ the standard library doesn't use exceptions for most errors. And it's common to avoid exceptions entirely (e.g. the google style guide forbids them).

So I'd say the role of unchecked exceptions is not too different from panics in many other languages, though the situation tends to be more messy in Rust due to legacy code and the lack of good alternative error handling mechanisms.


That's a difference in usage. In the default configuration, panics can be caught and thus are functionally identical.

The actual difference is that you can set panic=abort as the user of the code (it's a compiler parameter), in which case there is no unwinding and the program aborts immediately.

To me, the lack of possible handling and recovery (based on a compilation flag) is what arguably disqualifies it as an exceptions system.


Panics _can_ sometimes conceptually share the same implementation as exceptions, but given that their usage is not the same, and the fact that the ecosystem doesn't use panics as exceptions, it's a bit of stretch saying they're "functionally" the same.

A bit like saying trains and chairs are functionally the same since they both can have wheels; when there's the crucial difference that chairs don't go choo-choo.


> given that their usage is not the same, and the fact that the ecosystem doesn't use panics as exceptions, it's a bit of stretch saying they're "functionally" the same.

No, these don’t matter. Every langage with exceptions uses them differently, that doesn’t make Python’s no-exceptions and Java’s exceptions.

Regardless of how it’s used, it either is or is not an exceptions system. That’s what “functionally” means.

For instance Go’s panics are an exceptions system, you can always recover() and handle the exception during its unwinding.

> A bit like saying trains and chairs are functionally the same since they both can have wheels

No, it’s like saying an HSR and an ore train are functionally similar, because both are trains even though they’re used differently, in different contexts, and with different performance profiles.


You are arguing on technicalities. By your logic signals are also exceptions, and so is poking a screwdriver into the RAM port. Makes no sense.


On hw level, memory access errors and hardware malfunctions are often called exceptions. For example, on x86 the MCE architecture does this.


Windows exceptions are the equivalent of UNIX signals.

On hardware level exceptions are a synonym for traps.


Keep in mind that (although irrelevant at beginner level), if you’re writing any degree of Unsafe Rust, you need to ensure all your unsafe functions are exception safe, so they aren’t just a trivial concept.

https://doc.rust-lang.org/nomicon/exception-safety.html

Also, there is something similar to try/catch in Rust to catch exceptions, although the docs emphasize that you shouldn’t use for typical error handling. It’s mainly for when Rust is called from the outside (typically a C program). This is because you don’t want a Rust exception to unwind to external code, or all kinds of weird undefined behavior will happen.

https://doc.rust-lang.org/std/panic/fn.catch_unwind.html




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: