News for hackers. Stuff that matters.
Re:A new approach to error handling by layer8 on Tuesday July 30, 2024 @03:37PM (#41113311)
It is often important for the caller to know under what circumstances and for what reasons a function can fail, in order to react appropriately. In particular, callers want to distinguish between expected and unexpected errors, which can differ from use case to use case, and for expected errors they may want to distinguish between different expected cases, for example to decide whether it makes sense to retry the operation or not.

This doesn’t mean that the function signature has to expose implementation details. If the function is file_open(), listing FileNotFound as one of the possible errors doesn’t expose an implementation detail. If the function is sql_insert(), distinguishing between ConnectionError and UniqueKeyConflict is useful and doesn’t expose implementation details. Different error modes like that exist on all levels of an application.

Re:A new approach to error handling by wvenable on Tuesday July 30, 2024 @03:51PM (#41113442)
A function containing file_open() or sql_insert() will expose implementation details if it leaks those errors out.

I don't disagree but most of my application is not in those leaves; it's a call stack 20 levels deep.

Re:A new approach to error handling by layer8 on Tuesday July 30, 2024 @04:08PM (#41113589)
> A function containing file_open() or sql_insert() will expose implementation details if it leaks those errors out.

The correct thing to do in that case is to wrap/convert those errors to errors that are appropriate to that function’s interface contract. Exception types are an important part of a function signature, and should be designed accordingly, not be propagated blindly.

With unchecked exceptions, you are leaking implementation details dynamically, and you don’t even know it/can’t control it, because they are not statically checked. If you care about leaking implementation details, unchecked exceptions are arguably worse.

> I don't disagree but most of my application is not in those leaves; it's a call stack 20 levels deep.

I regularly have such failure mode distinctions in higher-level program logic as well. With unchecked exceptions, you often aren’t even aware what failure modes you have.

Re:A new approach to error handling by wvenable on Tuesday July 30, 2024 @05:15PM (#41114209)
> The correct thing to do in that case is to wrap/convert those errors to errors that are appropriate to that function’s interface contract.

Those, however, tend to be useless. What you end up doing is placing the real error untyped into new the exception/error and that's where the real meat is.

I can create an application that could retry an operation if it sees a particular kind of network error. Does it also work for the dozens of custom errors used by the interface contract of every single method? That's impossible to even know. How do I handle dozens of different SomeModuleError types?

> you often aren’t even aware what failure modes you have.

Again, that is the point of encapsulation and polymorphism. Maybe the implementation doesn't even exist at compile time in the case of a plugin.

I don't want to have to re-write my entire application every time some implementation detail changes anywhere. If you don't want to do that, then you're have to transform your errors/exceptions into something useless.

What I want to focus on what kinds of errors I can handle and where I can handle them. That has nothing at all to do with where the errors are generated.