rethrow

Incorrect stacktrace by rethrow

夙愿已清 提交于 2019-11-27 00:42:05
I rethrow an exception with "throw;", but the stacktrace is incorrect: static void Main(string[] args) { try { try { throw new Exception("Test"); //Line 12 } catch (Exception ex) { throw; //Line 15 } } catch (Exception ex) { System.Diagnostics.Debug.Write(ex.ToString()); } Console.ReadKey(); } The right stacktrace should be: System.Exception: Test at ConsoleApplication1.Program.Main(String[] args) in Program.cs:Line 12 But I get: System.Exception: Test at ConsoleApplication1.Program.Main(String[] args) in Program.cs:Line 15 But line 15 is the position of the "throw;". I have tested this with

Incorrect stacktrace by rethrow

北慕城南 提交于 2019-11-26 09:26:58
问题 I rethrow an exception with \"throw;\", but the stacktrace is incorrect: static void Main(string[] args) { try { try { throw new Exception(\"Test\"); //Line 12 } catch (Exception ex) { throw; //Line 15 } } catch (Exception ex) { System.Diagnostics.Debug.Write(ex.ToString()); } Console.ReadKey(); } The right stacktrace should be: System.Exception: Test at ConsoleApplication1.Program.Main(String[] args) in Program.cs:Line 12 But I get: System.Exception: Test at ConsoleApplication1.Program.Main

Best practices for catching and re-throwing .NET exceptions

给你一囗甜甜゛ 提交于 2019-11-25 22:49:35
问题 What are the best practices to consider when catching exceptions and re-throwing them? I want to make sure that the Exception object\'s InnerException and stack trace are preserved. Is there a difference between the following code blocks in the way they handle this? try { //some code } catch (Exception ex) { throw ex; } Vs: try { //some code } catch { throw; } 回答1: The way to preserve the stack trace is through the use of the throw; This is valid as well try { // something that bombs here }