Automatic "?" inside try {...} blocks


Note that C++ doesn’t have finally, since it has RAII, so I think it’s good and consistent for Rust to also not have it, for the same reason.


Everything you say is spot on. However, I stand by my assessment that it’s best to stick with existing conventions from other programming languages whenever it makes sense. That’s IMO also a form of intuitiveness. catch would be a good name choice if Rust existed in a vacuum. However, other languages have something similar and almost all call it try {}. Also, many use catch {} to wrap the error handling code. Deviating from these established meanings would only confuse people coming from other programming languages (i.e. confuse almost everybody). Familiarity is probably also the reason why enum was picked as keyword for a feature that is technically a “tagged union” not an “enum”. Not the same, but close enough.


I very much agree with this.

I just disagree with this :wink:. I would argue that languages with try ... catch would call what try { ... } does in Rust “catching”. The error handling code is referred to as “catch handlers”, or “handling”.

Musings on Rust's design It's interesting to think about the effect of Rust's design on the nature of error handling. There are two things catch handlers have to do in other languages to allow a fallible operation to converge (as opposed to diverging by throwing, returning, aborting, etc...) and allow execution to continue with the following statement: (1) ensure any variables that will be subsequently used are initialized, and (2) manipulate the state of the program to meet any post-conditions of the statement. In Rust, initialization tracking means that variables just won't be usable unless you ensure they are invariantly initialized. Also, since Rust is expression oriented, the post-condition an expression needs to meet to allow it to converge is to produce a value that matches its type. If the type of a fallible operation can represent both the success and failure cases, then no further work is needed to allow it to converge, so no catch handlers are needed.


Sure. Makes sense to call it “catching”. To clarify: With try I was talking about a try {} block, not about “to try”. I’ve edited my post and added curly braces to make it unambiguous.

closed #66

This topic was automatically closed after 6 days. New replies are no longer allowed.