Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
try..catch or try...catch..finally...?
 
which better if i want to use in my code..
Posted 31-Dec-12 21:27pm
Comments
Sergey Alexandrovich Kryukov at 1-Jan-13 2:28am
   
A totally incorrect question. No one is better. If you don't know how both work, you should not use any of them in your code. Why not just reading about it?
—SA
Sanjeev Alamuri at 1-Jan-13 2:32am
   
5+
Sergey Alexandrovich Kryukov at 1-Jan-13 2:35am
   
Thank you; I just put a complete answer.
—SA
Sergey Alexandrovich Kryukov at 1-Jan-13 2:36am
   
And this has nothing to do with ASP.NET, but you need to tag your platform and language.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question. You should use one form only where you need it, and another form only where you need it, and also try-catch form.
 
Just read and thoroughly understand: http://msdn.microsoft.com/en-us/library/s7fekhdy.aspx[^].
 
Catch is abused way to often. You should catch exceptions only on top of the frame stack of each thread, and in some rare special cases. You also should catch it in a main event loop of UI.
 
Please see 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[^],
Handling exceptions in class library (dll)[^],
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^].
 
Happy New Year!
 
—SA
  Permalink  
v2
Comments
Abhinav S at 1-Jan-13 2:43am
   
5 of course.
Sergey Alexandrovich Kryukov at 1-Jan-13 3:32am
   
Thank you, Abhinav.
—SA
prashant patil 4987 at 1-Jan-13 4:13am
   
gud +5
Sergey Alexandrovich Kryukov at 1-Jan-13 12:20pm
   
Thank you, Prashant.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

It isn't a case of "better" or "worse" it depends on what your code does.
If your code is writing data to a file for example:
try
   {
   Files.WriteAllBytes(myData);
   }
catch (IOException ex)
   {
   MessageBox.Show(string.Format("Saving your data failed!\nThe system reported \"{0}\"", ex.Message));
   }
Then you do not need a Finally block.
If on the other hand you open a connection to a database:
SqlConnection con = new SqlConnection(strConnection);
SqlCommand cmd = null;
try
   {
   con.Open();
   cmd = new SqlCommand(...
   }
catch (Exception ex)
   {
   MessageBox.Show(string.Format("Saving your data failed!\nThe system reported \"{0}\"", ex.Message));
   }
finally
   {
   if (con != null)
      {
      con.Close();
      }
   if (cmd != null)
      {
      cmd.Dispose();
      }
   }
Then a finally block allows you to make sure it is closed regardless of what happens.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If you've got some code that must execute (error or no - error) go ahead and use Finally.
That's generally my approach.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 520
1 Mathew Soji 335
2 BillWoodruff 260
3 OriginalGriff 215
4 Afzaal Ahmad Zeeshan 188
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100