using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; namespace UsageLibrary { class TestsRethrow { static void Main() { TestsRethrow testRethrow = new TestsRethrow(); testRethrow.CatchException(); } void CatchException() { try { CatchAndRethrowImplicitly2(); } catch (Exception e) { Console.WriteLine("{0}Implicitly 2 specified:{0}{1}", Environment.NewLine, e.StackTrace); } } void CatchAndRethrowImplicitly2() { SqlDataReader dr = null; try { SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=AdventureWorks2008R2;Integrated Security=True"); SqlCommand commd = new SqlCommand(); commd.Connection = conn; commd.CommandType = CommandType.Text; commd.CommandText = "select [Name] from [AdventureWorks2008R2].[Sales].[Store]"; if (conn.State == ConnectionState.Closed) conn.Open(); dr=commd.ExecuteReader(); CatchAndRethrowImplicitly(); } catch //(Exception ex) { throw; } finally // <-- if comment this block, stacktrace content is reset. { dr.Close(); } } void CatchAndRethrowImplicitly() { try { ThrowException(); } catch (Exception e) { throw; } } void ThrowException() { throw new ArithmeticException("illegal expression"); } } }
Exception saved = null; try { } catch (Exception ex) { saved = ex; } finally { } throw saved;
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)