Welcome to AspAdvice Sign in | Join | Help

Difference: throw or throw ex?

Just for demonstrating, if you have classes in C# as follows:

using System;

namespace WindowsApplication2
{
	public class Class1
	{
		public static void DoSomething()
		{
			try
			{
				Class2.DoSomething(); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
	}

	public class Class2
	{
		public static void DoSomething()
		{
			try
			{
				Class3.DoSomething(); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
	}

	public class Class3
	{
		public static void DoSomething()
		{
			try
			{
				int divider=0;
				int number=5/divider;
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
	}
}

And you call:

Class1.DoSomething();

What's the difference if you rethrow the exception using plain 'throw;' or 'throw ex;'?

 

ANSWER:

If you use "throw ex;", The stack trace is something like:

System.DivideByZeroException: Attempted to divide by zero.
   at WindowsApplication2.Class1.DoSomething() in l:\vs.net-projektit\windowsapplication2\main.cs:line 15
   at WindowsApplication2.Form1.button1_Click(Object sender, EventArgs e) in l:\vs.net-projektit\windowsapplication2\form1.cs:line 103

 

But if you use just 'throw' instead of 'throw ex' to rethrow the same exception, that is:

public class Class3
	{
		public static void DoSomething()
		{
			try
			{
				int divider=0;
				int number=5/divider;
			}
			catch
			{
				throw ;
			}
		}
	}


And of course for all rethrow statements, the stack trace is now like:

System.DivideByZeroException: Attempted to divide by zero.
   at WindowsApplication2.Class3.DoSomething() in l:\vs.net-projektit\windowsapplication2\main.cs:line 46
   at WindowsApplication2.Class2.DoSomething() in l:\vs.net-projektit\windowsapplication2\main.cs:line 30
   at WindowsApplication2.Class1.DoSomething() in l:\vs.net-projektit\windowsapplication2\main.cs:line 15
   at WindowsApplication2.Form1.button1_Click(Object sender, EventArgs e) in l:\vs.net-projektit\windowsapplication2\form1.cs:line 103

See the difference? BTW: This concerns both C# and VB. VB docs state that in "Throw expression" the expression would be required. This is not completely true, you can use just "Throw" in VB to rethrow the same exception. C# docs do tell that in "throw expression" the expression "is omitted when rethrowing the current exception object in a catch clause".

Sponsor
Published Thursday, April 15, 2004 7:12 PM by joteke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Difference: throw or throw ex?

Good examples! It is nice to know that.
Thursday, April 15, 2004 7:45 PM by joteke

# re: Difference: throw or throw ex?

What if you don't include the explicit declaration of the Exception since you aren't going to use it anyhow? Like this:

try
{
// Do Something
}
catch
{
throw;
}
Thursday, April 15, 2004 7:53 PM by joteke

# re: Difference: throw or throw ex?

Yeah, correct. Compiler complains about it because ex isn't used. I didn't care about it as the stack behaviour was my main concern. Thanks! I did fix the code.
Thursday, April 15, 2004 8:09 PM by joteke

# re: Difference: throw or throw ex?

Do you know what the underlying cause of these two different behaviors is? The same call stack exists, so does the explicit retrhow selectively choose what shows up in the call stack?

BTW, Great post!
Sunday, June 20, 2004 1:52 AM by joteke

# Rethrowing exceptions and preserving the full call stack trace

Did you know that depending on the way you rethrow exceptions you may lose important information? There

Wednesday, January 02, 2008 7:21 AM by Fabrice's weblog

# Exception Handling Problem | keyongtech

Sunday, January 18, 2009 12:14 PM by Exception Handling Problem | keyongtech

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below