Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is has panics which are functionally equivalent to (unchecked) exceptions.


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


An exception can be caught


A panic can be caught with catch_unwind. https://doc.rust-lang.org/std/panic/fn.catch_unwind.html


However you can set panic=abort in which case no unwinding takes place.

This makes panics completely unreliable for error reporting.


Eher depends on what you want from an exceptions mechanism.

There has always been a debate what exceptions are for. Python leans heavily towards "exceptions don't have to be exceptional, they can be normal control flow", while Rust is in the other side of the spectrum closer to "if it's something you might want to handle it's not exceptional enough to be an exception". Java is somewhere in between. All of them are valid view points.

A language without reliable exception mechanism is C, where even the most unusual errors are communicated through return codes or global flags that you are expected to check manually


> Eher depends on what you want from an exceptions mechanism.

Not really. If it instantly terminates the program without recourse, it's not an exception system: by definition, exceptions come with exception-handling of some sort.


When using panic=abort you can still report the error via the panic hook, you just can't recover from it.


A Rust panic can be caught, too.

It's just bad form to do so in Rust because it's bad for maintainability to design programs like that.

Rust is opinionated and very much NOT an anything goes language. There is a culture and the culture says do not catch panics. If you feel the need to catch a panic, make it a regular value instead.





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

Search: