Skip Navigation

Why I Prefer Exceptions to Error Values

cedardb.com Why I Prefer Exceptions to Error Values

Exceptions are often a better way to handle errors than returning them as values. We argue that traditional exceptions provide better user and developer experience, and show that they even result in faster execution.

51

You're viewing a single thread.

51 comments
  • My anecdotal experience is that Rust code, for example, has more calls to unwrap than I’d like. The problem here is that simply unwrapping results will crash the program on errors that could have been a user-visible error message with exceptions.

    unwrap() is explicitly not handling the error in a Result type. If you must do this, then at least use except(), to unwrap the code but with an error message if program crashes. Its the equivalent of having Exceptions and then not handling that exception. Therefore your critique is not valid here.

    One problem with Exceptions is, you never know what code your function or library calls that can produce an exception. It's not encoded in the type system or signature of the function. So you need to pray and try catch all possible exceptions (I look at this from Pythons perspective), if you don't want a Catch..All, which then you wouldn't know what error this actually is. And you still don't know where this error came from or happened in the code, how deep in the function call chain? Instead Errors as Values means its encoded in type system and you can directly see what errors the function can cause and (in Rusts case) you must handle the error, otherwise program won't compile. You don't need to handle anything else in this context. Compiler ensures that all possible errors are handled (again within context of our discussion). Vast improvement!

You've viewed 51 comments.