DEV Community

Discussion on: Exceptions are meant to be exceptional

Collapse
 
bbessa profile image
Bernardo Bessa • Edited

Good point...
Preparing our software to still rely with unexpected exceptions is crucial. But, as you referred, unexpected exceptions should be treated as something exceptional! There are few scenarios that are quite easy to identify those situations - depending on 3rd party systems/components, which we are abstracted about the implementation or using connections over the network, for example.
It would be interesting to consider a post which defined some guidelines when to think about a try...catch block to handle some unexpected behaviours. I'd be glad to help on that.

Collapse
 
jpswade profile image
James Wade

You're right. Exceptions are more important when you're using third party components because unexpected behaviour is, well exceptional to us.

It's difficult to handle unexpected behaviours, because they would be expected.

You shouldn't use a try-catch for unexpected behaviours, those should bubble up to the exception handler, rather than being caught by your code.

It's excepted behaviour that require a try-catch, for example, a failing connection is expected if the client throws an exception, in which case you might want to try something else when that happens.

Hope this helps to clarify.

Collapse
 
bbessa profile image
Bernardo Bessa • Edited

I think we are aiming on the same spot, but with different perspectives :D

When I mentioned unexpected exceptions, I'm talking about behaviours that are not controlled by us (as developers of our own source code). If you rely on a framework, or library, and you are not into developing on it, there are some behaviours that you can predict, but you can't improve - those are the kind of unexpected behaviours that I'm talking about.

All the exceptions thrown by your own source code should be treated as bugs, not as exceptions!

Exceptions shouldn't be seen as bug handlers, but failure management mechanisms to manage communication between different pieces of software.