DEV Community

Discussion on: Unchecked vs checked exception (with FP glasses)

Collapse
 
connorphee profile image
Connor Phee

Great article, thank you for taking the time to write it.

This is a difficult topic and as you mention always leads to questions about which to use. I have also heard this strategy:

Use an unchecked exception if you cannot recover, use a checked exception if you can. This is quite complicated to me because you then must decide for your consumer what is fatal and what is not, and the answer is likely not the same for all. Along with that, if something is recoverable or expected, an exception may be the wrong way to go.

Have you heard of this take before? Curious on your thoughts here.

Collapse
 
eureka84 profile image
Angelo Sciarra

Hello, thanks for the appreciation.
Well, to be honest, I have never heard of this perspective on the subject.
Let's say that I understand the point on checked exception, it's mine too, I can also see the point on unchecked even if, as you say, it's difficult to decide on behalf of your consumer on what is fatal or not and I would also argue that if an event is indeed fatal it should modelled as an Error more than a RuntimeException (given it's fatal nature), leading again to the conclusion that RuntimeExceptions are in the end GOTOs in disguise.

Collapse
 
connorphee profile image
Connor Phee

Thanks for the response. I agree an error could be better than an exception there, and also gives the consumer more control. Determining behavior for not only what you control, but also what your consumers control makes developing anything meant to be for generic use difficult.

Thanks again and great post!