Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
I read some articles about try/catch blocks.

Overall, try-catch is a useful statement and one of the good concepts in programming.

But someone saying that there are some performance issue with using the try/catch blocks in our code. Is it true?

Is there any issue using try catch in iOS(especially in limited memory apps like mobile applications) applications?

Using try/catch is a good practice?

Please help/Suggest me in this.

Thanks in Advance.
Posted
Updated 19-May-11 20:37pm
v2

1 solution

"Using try/catch is a good practice?" Not just this.

Not using try/catch is impossibly bad practice. The only acceptable alternative is not caring about errors and other exceptional conditions (yes, this is quite acceptable in some cases).

Now, try/catch can not be an issue of performance by definition. If you need exception handling, there are unavoidable, if you use any other way of error handling (I hate even mentioning the idea), you make performance much worse.

Nevertheless, I see that try/catch pretty often present huge performance problem. Yes, contradictory paragraphs in action. What I say in the previous paragraph assumed that the code is written by non-idiots. In real life, however… well, you understand. :-)

Try/catch should be used quite rarely. Ideally — once on the top of the stack of each thread (to prevent termination) and also in the main event cycle of the UI. Throw should be uses every time exceptional situation happens (not always an error, some exceptional situations, as the name suggests; classical example: "nuclear reactor overheated"). We do not spare CPU time for really exceptional situations, right. Another case of try/catch — primary specialize processing of the situation; try/finally should be more typical; close file handles, dispose, etc.

Actually, structure exception handling saves a lot of performance. Programming should be more offensive and less defensive. Most checks are not needed.

See some of my recommendations in my past answers:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Unhandled Exception : Access Violation[^].

Only last answer was about C++, but most recommendations are applicable to many platforms/languages.

—SA
 
Share this answer
 
Comments
R@jeev K R 20-May-11 3:38am    
Good explanation. Thanks for your detailed explanation....
Sergey Alexandrovich Kryukov 20-May-11 4:43am    
You're very welcome.
If so, please formally accept this answer (green button).
Thank you.
--SA
Sergey Alexandrovich Kryukov 20-May-11 23:32pm    
I see, thanks for sharing.
You second article discusses exactly what I mean: a sample of absolutely idiotic abuse of exceptions. A good example of how even the best technologies can be abused in most destructive ways!
--SA
R@jeev K R 23-May-11 1:22am    
:) You are right. The comments of that article give us more information :).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900