|
Hey i wanna start a discussion on try catch blocks. I read a lot of conflicting opinions on wether or not to use them and where or where not to use them, I just want a diffinative answer. Cheers.
|
|
|
|
|
Always use them around any code that may throw an exception that you need to deal with.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hey the thing is I know exactly when and where to use them it just seems that all through my code there are 100's of try catch blocks and I read very conflicting articles on wether or not this is best coding practice.
|
|
|
|
|
Mickt1985 wrote: I read very conflicting articles Which just means that everyone has their own opinion. It's up to you to choose which one you think is right.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Although everyone have their own opinion but the main important thing is user should not experience unexpected behavior. if there is something going wrong you need to catch and that could be possible through try..catch and logging the messages.
so resolving errors are more important then best practice... keep coding...
Thanks
-Amit Gajjar (MinterProject)
|
|
|
|
|
I understand that with tools like Codeit.right and The Visual Studio Analysis that Try Catch(s) are to be used sparingly and to catch only certain types of errors. You should try to wirte tests into the code in order to narrow the parameters of the code so that you will be able to report back to users problems. Hiding errors in Try / Catch in not the proper use of the machanic.
Hope this sheds some light on the use.. Try running the code by removing all the Try/Catch and if there is a better way like a If/Then that can cover the error then it is so much better.. in Addition Try/Catch are generally slower.
Regards.
|
|
|
|
|
Thanks very much for your reply.
|
|
|
|
|
If you are interested in executing a certain block of code irrespective of whether an exception occurs or not and still worry about catching an exception you might use the try-catch-finally block. Even if a goto statement is present in a try block, the control gets transferred to the label in the goto, only after executing the finally block.
You should try and use them in your code as much as possible, especially if you suspect a particular error may crash your programme.
Comm100 - Leading Live Chat Software Provider
modified 28-May-14 1:49am.
|
|
|
|