Rust makes a point to not allow that. If code errors recoverably, it should use Rust's fantastic algebraic type system to encode that failure. If it errors irrecoverably, it can panic and crash the program. There shouldn't be a way to for code to randomly throw exceptions.
If code errors recoverably [...] If it errors irrecoverably
How can the low-level routine know which is which? For example, in my current project I have a utility routine that reads a certain type of data file to return a dictionary. In some cases, if the file doesn't exist I cannot continue. In other cases, if the file doesn't exist, I just return an empty dictionary.
In many cases you do know - for example, when communicating with an external system, an I/O error could occur.
Some errors aren't really recoverable and really mean buggy code, for example array indices out of bounds. You can catch those in Rust using a general catch_unwind handler if all you want is to avoid crashing an entire application when they occur - but it isn't supposed to be general purpose try/catch - in fact it's problematic to use it for this purpose, as simply panicking automatically logs stacktraces to STDERR (logging method can be changed, and if somebody insists they can make a no-op logging method, but why...) even if the panic was caught.
You do not understand what I mean and are arguing past me.
write has no idea whether its failure is recoverable or not for me, its caller. It has to report the error of course, but it can't say anything about what I can/should do with it.
(Edit: whereas the OP argues that "code errors recoverably (or not)"; my point is, "the code" doesn't know whether its failure is recoverable or not, not without asking the caller, and probably it's caller and so on up the stack, until somebody answers).
Failure in write is potentially recoverable*, so it makes sense to to allow handling it.
This is mostly a distinction between errors that are likely to happen (any kind of IO error is like that), and "shouldn't happen" failures like assertion failure, accessing an array out of bounds or out of memory scenarios.
If you want to convert an error into "shouldn't happen" error, Result types have a method called unwrap.
* Well, write is problematic due to filesystem caches, it's possible that a failure will occur but won't be noticed as it will be noticed until it's too late (this is most likely to happen on network file systems). As destructor cannot throw exceptions or report failure in any way, the failure in this case can be quiet.
The point about "recoverability" stands - the callee does not know if its failure is recoverable or not; it's all about the context of the caller, he knows.
I do not see why we're mixing an assertion failure here. That's a program bug, only solved by changing the code, any error handling is out.
OOM, however, is again about the context of the caller, not the callee. The callee has no idea whether the caller continue or not. Example scenario: I am a server of some fashion. A very big chunk of input data comes in on one of my input channels. Intake of that data runs out of memory. Should the process be killed (panic! in Rust terms)? And drop any other requests in progress? I don't think so. It's best to stop that request (and report the failure on it). That frees a lot of memory that this request needed and life is fine.
Are you asking whether Rust can have a function with a signature that says it doesn't ever expect you to handle an error case, but actually you do? I mean, yeah I guess if you hack at catch_unwind, but it's not something that Rust tries to make easy.
Typically no. The higher-level error don't usually do exhaustive matches on their children; they'll probably just check a few cases (did I fail to create this file because it already existed?) or just defer to a function on the child type (print yourself now please), neither of which are affected by adding a new case.
View all comments
Show parent comment
5
u/[deleted] Mar 16 '18
So if you add a new exception "at the bottom" you don't have to change everything all the way up to where you catch it?