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

> `unwrap()` is "handling" errors by crashing the program,

IIUC, panicking via unwrap() or expect() IS graceful error handling. It unwinds the stack and releases resources. We should be clearer because it's confusing to people less familiar with Rust who will confuse this with actually crashing and immediately terminating.

Personally, I think if there's nothing you can actually do to handle this error then unwrap() or expect() are the correct choices. There's no point writing more code just to bubble an error that you can't handle.



> IIUC, panicking via unwrap() or expect() IS graceful error handling. It unwinds the stack and releases resources.

AFAIK, only with the default panic=unwind mode. With the panic=abort mode, it aborts the whole process instead of unwinding.


panic=abort is only intended for embedded stuff, I think.


Nope, it's intended whenever you want this semantic, and do not want to pay for landing pads. Embedded is a common case where this is true, but it's not exclusively for that.


If the filesystem you are trying to open a log file on fails, only your logging thread needs to give up, not your whole program. If there is anything you can still accomplish, don't panic, carry on. Tesla let the logging exception prevent charging of the vehicle.


> If there is anything you can still accomplish, don't panic, carry on.

This is often poor advice, because continuing in a corrupt state can often be worse (and harder to debug) than a clean panic. E.g. it's better to crash than to overwrite a save file with corrupt data, or transfer money to the wrong account, or show one user data belonging to another user.


If we are talking microservices I agree, take down the service. I have written a lot of safety critical stuff, and the advice for a given address space (virtual or physical) has been throw on inputs that are out of range or otherwise bad. This makes you fail fast and notice stuff, but i makes the conglomerate of address spaces as a whole be horribly unreliable, all coming down all the time. As we have bevome less monolithic with little persistent data that is shared between concerns, it has become possible to kill and restart only the offending thread, service, hardware etc. The result is a degraded functionality with an alert that it has become so. Tesla shouldn't have just stopped, they should have alerte the user that maintenance was necessary and given up on logging those errors to the now failed flash.


Let's say you have server or daemon and you call some library function - if it was written with such careless approach, whole server will fail just because user record had invalid email address, or because some entered data was not in expected format.

Let's say you have CLI tool - instead of message "XX is a directory - file expected" your tool will just fail silently.

There are much more examples. Graceful shutdown is what we have to do, even laziest of us.


Validation would be an expected Error and handled by returning an Err() rather than using unwrap() or expect().

.expect(format!("{} is a directory - file expected")) gives exactly the behaviour you've described for a CLI tool.

Graceful shutdown means releasing locks, deleting PID files etc when encountering an unrecoverable error. Not just any error.


> Validation would be an expected Error and handled by returning an Err() rather than using unwrap() or expect().

When language allows you to make all errors "expected", it's really stupid to don't use it. Only reason is laziness.


I don't understand what you're trying to say here. Laziness has nothing to do with it. Returning a Result vs using .expect("this should never be missing or an error") is about whether your program can continue after this error.


No, panicking is not a correct way to handle programm execution, it's just "I don't want to care about this possible error case right now", nothing more. I gave you 2 examples and you preferred to ignore them or say "it's ok". Let's agree to disagree, I will never understand people who are too lazy to allow panicking in their code and say "works as intended".

Many languages don't have Result types to return so there you can't do much sometimes to prevent data corruption or something worse. But in languages where you can return more than just "false", it's not acceptable.




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

Search: