using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace ThrowPerf { class Program { static void Main(string[] args) { var stp = new Stopwatch(); stp.Start(); for (var i = 0; i < 10000; i++) { try { // base cost // try { doTry(); goto failed; } // catch failed: { doCleanup(); } } catch { } } stp.Stop(); Console.WriteLine("Method call+goto: {0}", stp.Elapsed); stp.Reset(); stp.Start(); for (var i = 0; i < 10000; i++) { try { // catch try { doTryFail(); } catch { doCleanup(); } } catch { } } stp.Stop(); Console.WriteLine("Try/Catch: {0}", stp.Elapsed); stp.Reset(); stp.Start(); for (var i = 0; i < 10000; i++) { try { bool suceeded = false; // finally try { doTryFail(); suceeded = true; } finally { if (!suceeded) doCleanup(); } } catch { } } stp.Stop(); Console.WriteLine("Try/Flag/Finally: {0}", stp.Elapsed); stp.Reset(); stp.Start(); Console.ReadLine(); } // the purpose of checking datetime is just to ensure the call does // not get optimized out. Otherwise, it does not matter. private static void doTryFail() { if (System.DateTime.Now.Year == 0) Console.WriteLine("foobar"); throw new NotImplementedException(); } private static void doCleanup() { if (System.DateTime.Now.Year == 0) Console.WriteLine("foobar"); } private static void doTry() { if (System.DateTime.Now.Year == 0) Console.WriteLine("foobar"); } } }