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

The way Rust does it is not perfect, but it's the best I've used so far, IMO.

Rust, if you are not familiar, has a special operator that let's you call a function that returns Result and automatically unwrap it and return early if it's an Error rather than Success. It will even automatically CONVERT the error for you if you've defined the proper trait implementation to convert the one error type into the other.

So, in Rust the boilerplate happens ahead of time (implementing the conversion trait), outside of your function's internal logic. In your function you just write:

    fn foo(): Result<String, Error> {
        let f = something_that_can_faile()?;
        uses_the_success_value(f)
    }
No try{}catch{}, no match statements, nothing. Just a question mark.

Swift also strikes a nice compromise, IMO. It just has a throws tag like C++, except it actually works.

I never considered a warning-level check for Java's checked exceptions. That sounds like a nice idea at first blush.




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: