DEV Community


Discussion on: Exceptions are meant to be exceptional

jpswade profile image
James Wade Author

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.

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.