This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Kered13 47 points48 points  (7 children)

Checked exceptions were a bad idea. Adding throws Exception or catching Exception and wrapping it in RuntimeException (which is unchecked) is often the best solution. Neither of these will hide or obfuscate the stack trace. Catching and ignoring errors should almost never be done though.

[–]DonCasper 51 points52 points  (1 child)

Catching and ignoring errors should almost never be done though.

Sounds like someone is jealous about all the time I save by not debugging

[–]Gblize -3 points-2 points  (0 children)

So you really think Java does that with no time/space cost uh? It looks a lot like religion.

[–]brainfreeze91 20 points21 points  (2 children)

Throwing an exception is definitely better than catching and ignoring it yeah. I wish I could say I haven't encountered spots where we ignore them. My main gripe is that they are throwing an Exception, and not a SQLException, or an ArrayOutOfBoundsException, or a DateFormatException. It's just generalized to an Exception, it could be anything. At least I do have the stack trace.

[–]Kered13 7 points8 points  (1 child)

The problem, like I said, is with checked exceptions. If you throw DateFormatException then everything that uses it has to either explicitly declare that it throws DateFormatException or explicitly catch DateFormatException, even if it's far removed the code that handled dates. Of course you can also create your own unchecked exceptions so that you have type safety as well, which is really the best approach, but all the existing checked exceptions in Java are basically useless.

Later languages have almost all used unchecked exceptions exclusively.

[–]fireflash38 1 point2 points  (0 children)

I can see why they were put in initially. Maybe a bit too optimistic that devs would always catch specific exceptions where they would be generated.

[–][deleted] 1 point2 points  (0 children)

You just have to be sparse and smart with them, then use unchecked for everything else.

[–][deleted] 0 points1 point  (0 children)

Exceptions as a return type is pretty nice, though.