Live data from Hacker News

Why checked exceptions failed

borretti.me

311–318 of 318 posts

Re: Why checked exceptions failed

#311

Earlier quoted context omitted.

Situations in which you must raise, propagate and handle exceptions should be rare.

Parsing input, handling I/O is a good chunk of what a lot of run of the mill software does. Since this is Java and exceptions are the main way of expressing fallibility, I’m at a loss as to what should code that needs to indicate fallibility should be using other than exceptions.

Return values obviously, with suitably designed method contracts. Some (maybe most) types of I/O errors are proper exceptions. Parsing failures are not.

Re: Why checked exceptions failed

#312

Earlier quoted context omitted.

He means exceptions are not for common control flow that shows up everywhere, like a return value. You want an exceptional control flow change when your hard drive runs out of storage, but you don't want exceptional control flow change when your hashtable doesn't have the key you're looking for. The categories and frequency of these conditions are very different.

Hmmm, sounds like it just transforms into a Sorites paradox, no?

I don't think cases encountered in real work are really that unclear. It can sometimes seem unclear if you're used to language semantics that are inherently riddled with possible error states (like languages that allow null).

Re: Why checked exceptions failed

#313

Earlier quoted context omitted.

They aren't simple to express and propagate? What's so hard about declaring throws?

You have to think about exception polymorphism. If you don't have exception inference, you also have to think about manually propagating exception contracts. Declaring it once is not a big deal, doing it over and over again is super annoying.

That's great, think about it...

Let's say I have a Collection and want to do IO within that collection... If IOException was a runtime exception I could just write that code without handling it and an unsuspecting user of my new IOCollection would suddenly get an IOException. That means I need to explicitly deal with it in my class and can't add a serious exception to the behavior of the class.

Yes, it can still throw a runtime exception which is why the separation of the two is so important.

OTOH we have InputStream and OutputStream. Both throw an IOException for all their methods. So if I have one of them I should always handle the exception which is always the right thing to do...

But you might say, wait... What if I have a theoretical InputStream that will never throw an IOException?

Don't fret, we have that. It's called a ByteArrayInputStream and works roughly like this:

  ByteArrayInputStream bos = new ByteArrayInputStream(new byte[100]);
  int value = bos.read();
Notice I didn't use try and catch. Why? Because neither the constructor nor the read method throw an IOException which is legal in Javas polymorphism implementation. However, the following code won't compile without a catch exactly because I need to handle IOException for the generic case:

  InputStream bos = new ByteArrayInputStream(new byte[100]);
  int value = bos.read();

Re: Why checked exceptions failed

#314

Earlier quoted context omitted.

Parsing input, handling I/O is a good chunk of what a lot of run of the mill software does. Since this is Java and exceptions are the main way of expressing fallibility, I’m at a loss as to what should code that needs to indicate fallibility should be using other than exceptions.

Return values obviously, with suitably designed method contracts. Some (maybe most) types of I/O errors are proper exceptions. Parsing failures are not.

So now you have two different error paths, one in which the file can't be accessed, and one in which the file can be accessed but the contents are corrupted? The developer must handle exceptions and they must still check if the function returned some sentinel error value?

What's the point of forcing two different error paths on the developer?

Re: Why checked exceptions failed

#315

Earlier quoted context omitted.

> Ditto @Nullable and @Nonnull. I was referring to the annotations. My proposal moots them. References would still be nullable.

Can you explain why it is good that null is a valid value for your types at the same time as you are trying to prevent them from ever containing that value?

You're overthinking this.

Semantics of 'null' are the same. Underlying implementation is different, better.

Re: Why checked exceptions failed

#316
post #314

Earlier quoted context omitted.

Return values obviously, with suitably designed method contracts. Some (maybe most) types of I/O errors are proper exceptions. Parsing failures are not.

So now you have two different error paths, one in which the file can't be accessed, and one in which the file can be accessed but the contents are corrupted? The developer must handle exceptions and they must still check if the function returned some sentinel error value? What's the point of forcing two different error paths on the developer?

They are different classes of errors, so you want different error paths. Parsing errors are common and should be handled locally with all of the usual logic. Exceptions are not supposed to be common errors which is why they trigger non-local control flow.

Re: Why checked exceptions failed

#317

Earlier quoted context omitted.

Can you explain why it is good that null is a valid value for your types at the same time as you are trying to prevent them from ever containing that value?

You're overthinking this. Semantics of 'null' are the same. Underlying implementation is different, better.

If the semantics were the same, you wouldn't need a replacement and all your proposed changes would have no observable effect on the behavior of any program.

Re: Why checked exceptions failed

#318

Earlier quoted context omitted.

You're overthinking this. Semantics of 'null' are the same. Underlying implementation is different, better.

If the semantics were the same, you wouldn't need a replacement and all your proposed changes would have no observable effect on the behavior of any program.

Semantics != behavior.

In truth, I'm having hard time deciding if you're trolling or actually truly, sincerely not understanding this.

Read the wiki page. Implement a few Null Objects. If you still have questions, about this trivial concept, then I'll be happy to go a few more rounds.

Post reply on HN