Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
shall i write try-catch in finally block.. is it possible??????????
Posted
Comments
[no name] 16-Mar-15 2:28am    
What you have tried?

Syntactically it is possible - however a try-catch block is very expensive so you should not use it except there is no other way to catch malfunctioning...The results of this survey[^] may give you ideas where to use and where not...
To be more specific you must share the exact case you intend to use nested try-catch blocks...
 
Share this answer
 
Comments
[no name] 16-Mar-15 2:42am    
If you asked me for an in-depth explanation I would have to cite some blogs etc. but all that I've read says try-catch-blocks themself are *cheap* (at least in the current implementation of .Net). It just becomes expensive if an exception is actually being thrown because the stacktrace needs to be unwound.
- Sebastian
Kornfeld Eliyahu Peter 16-Mar-15 2:55am    
Which means that it is expensive to catch for instance wrong user input! If you do so you actually USE try-catch to check if the input is valid where you can do that with some if structure...
Any peace of code is 'cheap' if you do not run it...
[no name] 16-Mar-15 3:04am    
Peter, I took your answer literally. You said a try-catch-block is expensive. That's not correct to my knowledge. You didn't take my answer literally, you implied the inquirer (or I?) would abuse exceptions for validation, which isn't stated anywhere. :-)
- Sebastian
Kornfeld Eliyahu Peter 16-Mar-15 3:09am    
I didn't mean to abuse or took any... :-)
All I say that to say try-catch is cheap is only true if you actually use none! An use none is means that you never hit the catch...But if you now you never hit the catch, why to use it in the first place? And if you have any chance to hit it than you WILL hit it - so it is expensive...
It of course a play around philosophy, but what I'm trying to say, that do not use try-catch without reasoning it very well...
[no name] 16-Mar-15 3:33am    
I would like to change that to: Try-catch and even nested try-catch are cheap if exceptions are exceptional. If they *are* exceptional and you do hit the catch-block then it doesn't matter that this one exception is expensive because something went so wrong that user interaction or -notification is probably required and users are slow :-)
Yes, it's possible. Whether you actually should do it I can't tell because I don't know what you're trying to accomplish.
 
Share this answer
 
There is more good advice here:
http://stackoverflow.com/questions/4799758/are-nested-try-catch-blocks-a-bad-idea[^]

Refactoring may be an option.
 
Share this answer
 
Comments
NagaNimesh 11474558 16-Mar-15 3:25am    
tanq so much to everyone ..
sorry i did'nt ask the ques clearly..
my task is to place a try catch in finally block..which mean that there should be only one finally block in a method ???? or shall i place n number of finally blocks in a nested finally block???? but i got some clarity by your suggestions...tanq to all
[no name] 16-Mar-15 3:41am    
No offense, please, but I think you really should read up on the basics.

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