News for hackers. Stuff that matters.
Re:Checked exceptions: Java’s biggest mistake (2014) by bcrosby95 on Friday September 11, 2020 @10:35AM (#24443270)
The biggest problem with checked exceptions is that the application writer decides what is a recoverable error. Not the library designer. I could easily imagine a program where SQLException is a recoverable error. But I don't write those types of programs.
Re:Checked exceptions: Java’s biggest mistake (2014) by leetcrew on Friday September 11, 2020 @10:46AM (#24443380)
at a high level, doesn't this make sense though? the library author knows whether their code can recover from a certain exception, but they don't know how important their library is to the client application. in your SQL example, if the application's whole purpose is to track inventory using that database, then yeah, SQLException is probably unrecoverable. but if the database is only needed for some ancillary functionality, the application writer probably just wants to tell the user "hey, this feature isn't available right now" and let them keep on using the rest of the program. maybe I'm missing some context here? I'm primarily a c++ dev, and I don't know much about SQL either. in my project, a crash is only acceptable if we are out of memory or if data corruption is imminent.
Re:Checked exceptions: Java’s biggest mistake (2014) by merb on Friday September 11, 2020 @10:44AM (#24443365)
well the problem is that SQLException is not a good exception at all. it actually includes the following errors:

    - database connection closed
    - SQLTimeoutException (o.O)
    - protocol exceptions like duplicated key
    - query exceptions
the problem is that some of them are recoverable but usually it's more of an hassle and duplicated key exceptions are easier to handle in app code via upsert, etc.

so basically the problems are library designer errors and not application writer errors.

checked excpetions should only be exceptions that a developer COULD handle and not ones that he can't

in C# the library designers usually create a "Result" object that has a boolean of succeded or status of enum instead of using exceptions for failures. i.e. most i/o errors are not recoverable, thus c# does not enforce you to recover from them.

Re:Checked exceptions: Java’s biggest mistake (2014) by cletus on Friday September 11, 2020 @11:08AM (#24443629)
Yeah it's reasonably common for people to try and salvage checked exceptions by saying people are doing them wrong but at some point you realize the best you're doing is trying to put lipstick on a pig.

Here's a bigger problem: checked exceptions pollute your API and expose implementation details. Consider an API that stores and retrieves objects. A particular implementation does so by writing them to a database via JDBC so you get SQLExceptions. You have two basic approaches:

1. Include SQLException in your function signatures so the caller can deal with it. This exposes the implementation detail; or

2. You can hide it by transforming that checked exception into something specified for your API. At this point, what benefit have you gained from SQLException being a checked exception? You're hiding that detail.

For (1), you're baking checked exceptions into your function signatures such that it can be really difficult to change later on.

If you sit down and think about the practicalities the argued upsides of checked exceptions are essentially nonexistent and unchecked exceptions are actually strictly superior.

Here's another pattern that happens with checked exceptions in Java:

    try {
      doSomeSQL();
    } catch (SQLException e) {
      // do nothing
    }
You will see people do this all the time because they don't want to deal with the checked exceptions. A better catch-clause is:

    throw new RuntimeException(e);
You can argue people shouldn't do the first and they shouldn't but unchecked exceptions will simply bubble up unless you deliberately swallow it. That's a way better default. Defaults matter.
Re:Checked exceptions: Java’s biggest mistake (2014) by Mindless2112 on Friday September 11, 2020 @12:47PM (#24444614)
> checked exceptions pollute your API and expose implementation details

Exceptions aren't an implementation detail. They're part of the API contract. Checked exceptions make this explicit, but people don't like that because error handling is hard and pretending that errors don't happen is easy.

Re:Checked exceptions: Java’s biggest mistake (2014) by merb on Friday September 11, 2020 @01:17PM (#24444983)
I argue against that. because the problem is not that error handling is hard, the problem is that it is mostly unnecessary. Basically if you would write a hello world that writes directly to STDOUT you would need to handle IOException, guess what? it's basically useless to handle any kind of IOException in that case, because guess what? if your i/o device for output to display is broken you can only let your program crash. and that is the most common case how most exceptions are handled. and thats excalty where the k8s hype is about.

fail fast, start from scratch. you can't handle a network error in your application, but you can basically kill everything that lost network access to your database and recreate it where database access is still possible. but you don't need to do that in your application, your infrastructure should handle these and thats why most checked exceptions in the java stdlib are basically stupid. 90% of the stuff reuses exceptions for ease of use but 90% of the stuff should be runtimeexception and 90% of them should split them up, between stuff that I COULD recover from and from stuff that I can't (i.e. SSLException should be split into validation of certificate exceptions and protocol violations!!! that mostly can't be handled without reconfiguring the JVM!!!! I mean there is a SSLProtocolException but it's basically useless since it reports a totally different thing..)

Re:Checked exceptions: Java’s biggest mistake (2014) by zeroimpl on Saturday September 12, 2020 @12:31AM (#24449858)
I've literally never seen somebody catch a SQLException and do nothing with it - you quickly learn that just doesn't work, and it would never pass a code review.

With respect to exposing implementation details:

If the caller is the one who passed in the connection, then it makes complete sense to throw a SQLException so the caller can deal with it.

If the connection was opened in your method, then yes you probably should wrap it, handle it properly and clean up the connnection, and then throw a different exception.

The only time it's ambiguous is if the connection came from an instance variable. That's often a poor design, but usually means it came indirectly from the caller and so it still makes sense to throw SQLException.

In general, SQLException works fairly well as a checked exception. If some code generates a SQLException, your transaction failed and you should generally abort it (or you can check the specific error code if you are prepared to handle anticipated failures such as a duplicate key error). If it generates any other exception, you can continue working with the database (such as saving the failure status to a table).

Re:Checked exceptions: Java’s biggest mistake (2014) by ragnese on Friday September 11, 2020 @01:05PM (#24444815)
> You can hide it by transforming that checked exception into something specified for your API. At this point, what benefit have you gained from SQLException being a checked exception? You're hiding that detail.

This is what you should do, yes. And I gained from SQLException being checked, because I am reminded by the compiler that I need to wrap it in my API's Exception value. If I can't handle it or don't think a caller of my code can/should handle it, I'll wrap it in an unchecked exception and rethrow.

> Here's another pattern that happens with checked exceptions in Java:

I mean... what do you want? You can't fix stupid. That's a really obvious mistake that should never ever make it past code review. I can shit on Java for making everything nullable and thus actually difficult to figure out if you should null-check something. But I can't shit on Java for someone writing code like that.

Re:Checked exceptions: Java’s biggest mistake (2014) by csharptwdec19 on Friday September 11, 2020 @11:11AM (#24443662)
> checked excpetions should only be exceptions that a developer COULD handle and not ones that he can't

And that's kinda the problem; you can declare the ones a user MIGHT be able to recover from, but there's still the chance of unrecoverable or unforseen errors, so you wind up declaring THROWS EXCEPTION anyway...

> in C# the library designers usually create a "Result" object that has a boolean of succeded or status of enum instead of using exceptions for failures. i.e. most i/o errors are not recoverable, thus c# does not enforce you to recover from them.

Depends on the operation but yes. Either There's a Try___ Pattern (where boolean is result, and 'out' parameter from method is your parsed value) or some will do an enumeration pattern.

Still, I'm a fan of Option for these sorts of things nowadays...

Re:Checked exceptions: Java’s biggest mistake (2014) by ragnese on Friday September 11, 2020 @01:00PM (#24444771)
I fucking hate SQLException. It's a giant pain in the ass to dissect it to figure out if it's an error because the database exploded, or because I made a syntax error, or because something real failed like a uniqueness or foreign key violation.
Re:Checked exceptions: Java’s biggest mistake (2014) by masklinn on Friday September 11, 2020 @02:03PM (#24445511)
> - protocol exceptions like duplicated key

> - query exceptions

Even that is just barely scratching the surface. Postgres has ~250 error codes, and while not all of them can be triggered by all operations or statements, there's way more granularity in there than there is in just two piddly exceptions.