Skip to main content
added 55 characters in body
Source Link
41686d6564
  • 19.8k
  • 13
  • 48
  • 85

Note: This answer is for .NET Framework. You might observe a different behavior if you're using .NET Core or .NET 5.0 and above, as mentioned in the comments. I did not test on all versions of .NET Core

Note: This answer is for .NET Framework. You might observe a different behavior if you're using .NET Core or .NET 5.0 and above, as mentioned in the comments.

Note: This answer is for .NET Framework. You might observe a different behavior if you're using .NET Core or .NET 5.0 and above, as mentioned in the comments. I did not test on all versions of .NET Core

added 173 characters in body
Source Link
41686d6564
  • 19.8k
  • 13
  • 48
  • 85
  • throw ex: Rethrows the exception from this point and resets the stack trace.

  • throw: Rethrows the exception from this point and maintainspreserves the stack trace.

WhyQ: Why "line 25"?

A: Because both throw and throw ex rethrow the exception from that point.

WhyQ: Why is there no difference in this case?

A: Because there aren't any more stack frames in the stack trace to reset.

HowQ: How can we see the difference?

OkayQ: Okay, you say both will throw the exception from that point. What if I want to maintain the original line number?

A: In this case, you can use ExceptionDispatchInfo.Capture(ex).Throw(); as explained in How to rethrow InnerException without losing stack trace in C#?:

  • throw ex: Rethrows the exception from this point and resets the stack trace.

  • throw: Rethrows the exception from this point and maintains the stack trace.

Why "line 25"?

Because both throw and throw ex rethrow the exception from that point.

Why is there no difference in this case?

Because there aren't any more stack frames in the stack trace to reset.

How can we see the difference?

Okay, you say both will throw the exception from that point. What if I want to maintain the original line number?

In this case, you can use ExceptionDispatchInfo.Capture(ex).Throw(); as explained in How to rethrow InnerException without losing stack trace in C#?:

  • throw ex: Rethrows the exception from this point and resets the stack trace.

  • throw: Rethrows the exception from this point and preserves the stack trace.

Q: Why "line 25"?

A: Because both throw and throw ex rethrow the exception from that point.

Q: Why is there no difference in this case?

A: Because there aren't any more stack frames in the stack trace to reset.

Q: How can we see the difference?

Q: Okay, you say both will throw the exception from that point. What if I want to maintain the original line number?

A: In this case, you can use ExceptionDispatchInfo.Capture(ex).Throw(); as explained in How to rethrow InnerException without losing stack trace in C#?:

added 173 characters in body
Source Link
41686d6564
  • 19.8k
  • 13
  • 48
  • 85

Note: This answer is for .NET Framework. You might observe a different behavior if you're using .NET Core or .NET 5.0 and above, as mentioned in the comments.


Okay, let me take a stab at it. The difference between throw and throw ex is already explained in Is there a difference between "throw" and "throw ex"? but I'll try to put it in more clear terms to fit the narrative of this question.

Okay, let me take a stab at it. The difference between throw and throw ex is already explained in Is there a difference between "throw" and "throw ex"? but I'll try to put it in more clear terms to fit the narrative of this question.

Note: This answer is for .NET Framework. You might observe a different behavior if you're using .NET Core or .NET 5.0 and above, as mentioned in the comments.


Okay, let me take a stab at it. The difference between throw and throw ex is already explained in Is there a difference between "throw" and "throw ex"? but I'll try to put it in more clear terms to fit the narrative of this question.

deleted 10 characters in body
Source Link
41686d6564
  • 19.8k
  • 13
  • 48
  • 85
Loading
Source Link
41686d6564
  • 19.8k
  • 13
  • 48
  • 85
Loading