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.
Why checked exceptions failed
311–318 of 318 posts
Re: Why checked exceptions failed
#312Earlier 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?
Re: Why checked exceptions failed
#313Earlier 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.
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
#314Earlier 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.
What's the point of forcing two different error paths on the developer?
Re: Why checked exceptions failed
#315Earlier 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?
Semantics of 'null' are the same. Underlying implementation is different, better.
Re: Why checked exceptions failed
#316Earlier 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?
Re: Why checked exceptions failed
#317Earlier 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.
Re: Why checked exceptions failed
#318Earlier 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.
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.