Should we just put every command and query in a try … catch block? I’ve done so in a web app telling myself that it won’t show any “Unexpected Error” to the user. After thinking about it, is it the best idea? Am I creating a monster that won’t log the proper error? The code in itself log every error coming up in the database, but does catching it a step before is good practice? Does it help ME do something with the error after? In need of some expert advice, I went to ask my favourite friend : Google.

Reading through some stackoverflow’s answers it seems obvious that it’s not the best way to go. But why so? People often wrote about it has being defensive programming and unless you can fix the problem causing the exception there’s no need, and no reason, to catch it. I felt a bit surprise about that way to see it, I learned in school that you need to catch exceptions and comment on everything. I do know that there’s a gap between what you learn and what you do in “real life”. So here I am, ambivalent between catching and letting my error go. One particular comment stuck with me; “Don’t nail your program into the upright position.” After further research, I found the phrase coming from Verity Stob on the site 97 things every programmer should know. I believed I made the best choice to write a personalize catch for the user and letting the code log the real error by itself.

The reason behind it may be quite simple, but to this day I’m still not sure if I should do this; I need to tell the user that something went wrong, because what they expect from the web app is currently not happening and I don’t want them to think that everything went right, in that optic, I prefer using the try … catch and use the catch to show a fitting error message. For now it’s the best way for me, there’s probably something better (no doubt about that) but my lack of knowledge will be my excuse. Now that the exception is caught, has been written once in the log what the heck should I do with it? How is it helping me fix it?

Well, I got an idea for that believe me or not, I figured that catching it may not do something helpful for the user, but it sure is helping me understand and learning what my code is doing. Because you know I’m still learning a lot right now and if something went wrong, well, that’s some great news! The logged error will help me find exactly where the code went full stupid (or did I?) and then I could … fix it then push the update. I do understand that we could say that the code is never repairing itself, but I’m not offering a logical and sustainable way to overpass the error to the user, I then feel that I’m not properly “fixing” it per se.

The problem here is that I’m inserting waiting time for the user between the time he is living the broken code and the moment the update is online, but is there any other way to go? Can I really be 100% sure every time I push code everything is working has intended? I don’t believe perfect code exists, but what a beautiful quest to at least try! For now, my try catch shall live in moderation, and maybe … maybe … I could automate some error to send me an email to speed up the process ….. what a mad idea…