News for hackers. Stuff that matters.
Re:Why Checked Exceptions Failed by scarface_74 on Saturday July 15, 2023 @09:12AM (#36736375)
Catching the root exception is not a bad idea. If you had caught the new exception what would you have done with it?

Most of the time, it’s either catch the exception, log it in a central logging system keep moving and have a central alerting system or catch the exception log it and crash the program.

Re:Why Checked Exceptions Failed by simias on Saturday July 15, 2023 @09:26AM (#36736524)
> Catching the root exception is not a bad idea. If you had caught the new exception what would you have done with it?

Something relevant to the error condition, probably?

There may be some cases where the total set of possible errors is too large to meaningfully handle every one of them specifically (for instance if you call a high level GPU initialization routine that can fail in a myriad of ways) but that's not true in all cases. Maybe the new error was in fact recoverable, or maybe it calls for some more specific diagnostics.

At any rate I completely agree with the parent that changing the set throwable exceptions should be considered a breaking API change and should be enforced by the compiler. If an API method wants to future-proof and be able to throw anything, it can declare just that and everybody will know to expect the unexpected.

That's why I vastly prefer Rust's Result<> system which does most of what exceptions do and with fairly similar ergonomics but with normal return values and all the type checking that it involves.

Re:Why Checked Exceptions Failed by scarface_74 on Saturday July 15, 2023 @09:40AM (#36736652)
> Something relevant to the error condition, probably?

And in every language I know you can have catch blocks for specific errors and then have a generic catch all.

Re:Why Checked Exceptions Failed by simias on Saturday July 15, 2023 @11:35AM (#36737745)
The ability to catch exceptions is not the issue, it's knowing what to do once they're caught that's the problem.

The parent apparently had exhaustive exception handling, catching all cases. A new exception is now generated, probably signaling a new error condition, you can't expect the calling code to be able to handle it gracefully.

Hence why a compile error might be a better solution here, the coder could decide whether the new exception can fit an existing handler or requires special handling.

Re:Why Checked Exceptions Failed by saurik on Saturday July 15, 2023 @12:52PM (#36738721)
Realistically speaking, you should essentially never try to "handle" errors with tricky program logic: propagate them up (automatic in languages with exceptions) and eventually--in as few places as possible--report them to the user so the user can decide what to do, not the code.
Re:Why Checked Exceptions Failed by simias on Saturday July 15, 2023 @01:02PM (#36738825)
I disagree, low level exceptions rarely make sense on their on for the users, the automatic propagation of exceptions just promotes laziness by having a catchall "print(exception); exit(1);" at the top level.

If you're lucky you get a proper stack trace that lets you figure out what's going on, but even that is poor ergonomics.

A high level library should report high level exceptions for instance, not minute details of the lower layers. Nothing is more frustrating that having a program fail and all you have is some cryptic "No such file or directory" or "Operation not permitted" error that doesn't tell you what the code was actually trying to do or give you any hint on how to fix it.

Re:Why Checked Exceptions Failed by breadwinner on Saturday July 15, 2023 @09:37AM (#36736621)
It is a bad idea according to Microsoft.

See https://learn.microsoft.com/en-us/dotnet/fundamentals/code-a...

Re:Why Checked Exceptions Failed by scarface_74 on Saturday July 15, 2023 @09:44AM (#36736682)
This is for a library. Whatever the “root” of your code is should have a generic catch all where you do “something”, you don’t want library code (either yours or the system) to decide for the user what should be done with exceptions.

As a library writer, you should do something about exceptions that you can and rethrow the ones you can’t,

Re:Why Checked Exceptions Failed by breadwinner on Saturday July 15, 2023 @09:54AM (#36736775)
> Whatever the “root” of your code is should have a generic catch all where you do “something”

If your only goal is to avoid crashing the app then you can catch the base Exception class at the root of you code and go home. But consider the scenario that you're writing a very important method, where the functionality of your method is very important. If your method does not do its job, the rocket may crash or the patient may die. In this case you want to know what recoverable errors are possible in the functions that your method calls, correct? Wouldn't be it nice to get a guaranteed-by-compiler list of possible, recoverable, errors?

Re:Why Checked Exceptions Failed by scarface_74 on Saturday July 15, 2023 @10:32AM (#36737136)
And then you have a list of catch blocks with the final one being the base.
Re:Why Checked Exceptions Failed by breadwinner on Saturday July 15, 2023 @11:51AM (#36737955)
No. You only catch exceptions you can recover from in your method. Other exceptions should be handled in higher layers.
Re:Why Checked Exceptions Failed by scarface_74 on Saturday July 15, 2023 @12:15PM (#36738234)
I’m talking about the “root” of your app
Re:Why Checked Exceptions Failed by revskill on Saturday July 15, 2023 @09:19AM (#36736458)
Catching at the root is only half of story. You must also have ability to resume where it was thrown.