Exceptions...

Anti-Rich

Well-known member
Joined
Jul 1, 2006
Messages
325
Location
Perth, Australia
Programming Experience
1-3
Hi all,

i have read that it is bad programming practice that when you are catching exceptions, to just catch a general exception. the question i have is, why is this such a bad thing? wouldnt it be more advisable to be able to handle a wider range of exceptions (especially ones you dont even know are coming),

if anyone could give me some feedback, it would be greatly appreciated

regards
adam
 
You should know every bit of the code good enough to anticipate and handle in a good manner most possible things that can go wrong - try at least! Use exception handling for those cases where it is not otherwise viable to handle it in code, those exceptional cases. For example when you know there is no way to be sure about what input you get from a user it is typical at some point to guard this with try-catch.

For example using a file open dialog to open an image file, and you get a corrupt file that most probably will throw an exception with image load. You don't analyze the internal bits of the file to determine yourself if it is valid, that would be an enormous waste of time and code.

In most code there is no need for catching exception that the method by documentation may throw because you know such scenario can never happen in the way you have implemented it.
 
cheers johnh for the reply. i do understand what you are saying, and it does make sense, when i think about it i usually do know what will or will not ever happen, you make a good point. i was also just wondering if there were any specific differences.. ie performance hits, etc.

thanks for the feedback, as always, much appreciated. :)

have a good one
adam
 
I haven't done any measuring on code with/out Try-Catch, and also not investigated and read further on this topic yet. I think "double-managed" code would perform worse than "single-managed". Catching errors that would normally make the app crash and end should be adding some kind of layer between the code call and its invokation. Since performance is important for programming I wouldn't think such layer was permanent, ie without Try the layer would let app crash and with Try the layer would catch it. Just thinking out load, I would say there are different code invokation paths for exception catching code and code without this net.

Another difference, if you Try all code inside every method it is actually harder to control the flow of the application. An important note here; Exception handling should not be used to control the flow of the application, it is just a mechanism to escape special and somewhat predictable situations where app otherwise may crash because of bad input to a method or bad processing within a method. As said, catching all code is not a good plan, because this lets you skip any analyze of internal code, consequence is if exception occur app continues without you the programmer really knowing where and what went wrong, thus not seeing how this will impact next steps/blocks of code. This may in turn lead to exception upon exception and an malfunctioning application that may not stop but be totally out of control. Many exceptions are not notified to the user of the application, so the user is also unaware about the bad condition of the running application. To step back to the basic example of the corrupt "image", if user was told right away that the "image" was corrupt, a new correct "image" would have been loaded. In some cases such scenario is hard to cover, but knowing the code and try-catch sparingly and intelligent give you full control at all stages of code execution, both for what happened and what to come.
 
ah ok, well i think im on the right track then, because the only time i really use try/catch is when im trying to cast/convert/parse something, and anything using an sqlconnection.

cheers for that john, much appreciated
have a good one
adam
 
There exist savers for that also, check into TryParse and TryCast.
 
Back
Top