DEV Community

Discussion on: Let's Clean Up: Ugly Try-Catches!

Collapse
 
deta19 profile image
mihai • Edited

Question, shouldn't try catch be used at the beginning of writing your code and after that you clean it up, because you know now that the code is stable?

Collapse
 
coly010 profile image
Colum Ferry

I'm not sure I fully understand what you are saying.

It sounds like you're saying you wrap the full app startup in a try catch and then come along later and remove it?

Try catches should be wrapped around any method you expect can throw an error at the layer you wish to handle it.

Perhaps you have three layers:

UI
Business
Data Layer

Your data layer may throw and error but you want to catch it at the UI level so you can show an error to the user.
Or perhaps, your business layer throws an error, but you want to catch it immediately so you can call a fallback method etc.