Kurt Posted November 30, 2003 Posted November 30, 2003 finally Is the working of the following two pieces of code equivalent(concerning the code that's executed no matter an exception is thrown or not)? Or is there a difference other than pure syntactical dispute? namespace Tests { using System; public class tryApp { private static void Main() { try { // some code that could throw an exception } catch { // handle the exception } finally { // run clean up code IN FINALLY BLOCK } } } } namespace Tests { using System; public class tryApp2 { private static void Main() { try { // some code that could throw an exception } catch { // handle the exception } // run clean up code OUT OF TRY BLOCK } } } Quote qrt
Moderators Robby Posted November 30, 2003 Moderators Posted November 30, 2003 (edited) In the first one the Finally block is always run. The second one will not if there's an error Edited December 1, 2003 by Robby Quote Visit...Bassic Software
Kurt Posted November 30, 2003 Author Posted November 30, 2003 Both seem to work exactly the same to me namespace Tests { using System; public class tryApp { private static void Main() { try { Console.WriteLine("Something is about to go wrong"); int a = 6; int b = 0; Console.WriteLine("Division = {0}", a/b); } catch { Console.WriteLine("Let me handle this"); } finally { Console.WriteLine("I always run..."); } Console.WriteLine("And does this run?"); } } } Quote qrt
Moderators Robby Posted December 1, 2003 Moderators Posted December 1, 2003 Sorry I had it in mind that there was a Throw New in the Catch block, then it would not reach the last line Quote Visit...Bassic Software
Kurt Posted December 1, 2003 Author Posted December 1, 2003 Thanx, I see now... very important when rethrowing exceptions.... namespace Tests { using System; public class tryApp { private static void Main() { try { Console.WriteLine("Something is about to go wrong"); int a = 6; int b = 0; Console.WriteLine("Division = {0}", a/b); } catch { Console.WriteLine("Let me handle this"); throw; } finally { Console.WriteLine("I always run..."); } Console.WriteLine("And does this run?"); } } } Quote qrt
Moderators Robby Posted December 1, 2003 Moderators Posted December 1, 2003 Now the line "And does this run?" will not run but the Finally block still does. Quote Visit...Bassic Software
Kurt Posted December 1, 2003 Author Posted December 1, 2003 Hi Robby, I started this thread under the Systax Related subjects for C#, with thread name 'finally'. It seems to be moved to the general pages, but there it got the thread name "DataView RowFilter Hell". I have been reading on the "DataView RowFilter Hell" thread today, but I did not start that thread, and it handled about complete different subjects. Now, I find our discussion under the general subjects with thread name "DataView RowFilter Hell", of which I would be the thread starter.... Any idea whats going on? Quote qrt
Moderators Robby Posted December 1, 2003 Moderators Posted December 1, 2003 Maybe you had accidentaly posted in that thread and a Mod split it into this one. I have since correvted the title. Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.