Though many people don't pay much attention to these two common terms being used in exception handling, in interviews, interviewers frequently ask this difference. So let's see the difference between the two.
Let's write the code that is responsible for producing an exception and we will look at the stack trace produced during exception handling.
We will be implementing the code using "throw" first. Please pay attention to Method2 of Class2.
- namespace ExceptionHandlingSample
- {
- class Program
- {
- static void Main(string[] args)
- {
- Class1 cls1 = new Class1();
- Console.WriteLine(cls1.Method1());
- Console.Read();
- }
- }
- class Class1
- {
- public String Method1()
- {
- try
- {
- Class2 cls2 = new Class2();
- return cls2.Method2();
- }
- catch (Exception ex)
- {
- Console.WriteLine("-----Stack Trace for the Exception Occurred------");
- Console.WriteLine(ex.StackTrace.ToString());
- Console.WriteLine("-------Method in which Exception Occurred------");
- Console.WriteLine(ex.TargetSite.ToString());
- }
- return "";
- }
- }
- class Class2
- {
- public String Method2()
- {
- try
- {
- FaultyMethod2();
- return "Method2 called";
- }
- catch(Exception ex) {
- throw;
- }
- }
- //This method is responsible for the exception
- public void FaultyMethod2()
- {
- throw new TimeoutException();
- }
- }
- }
In Method1, we call Method2 of Class2 using object cls2 that further calls FaultyMethod2 that produces the timeout exception.
The output of the preceding code is as follows:

As we can see in the stack trace, we have a full hierarchy, in other words FaultyMethod2, then Method2 and then Method1 along with the line numbers at which an exception occurred or was thrown. Also, the target site is given as "FaultyMethod2".
Now let's change the "throw" to "throw ex" in Method2 and see the difference. For making this change, replace the catch block in Method2 with the following:
- catch(Exception ex) {
- throw ex;
- }

Now please notice that in the stack trace, we see hierarchy as Method2, then Method1 along with the line numbers at which an exception is thrown. Also, the target site is given as "Method2".
Thus, after looking at the output of the "throw" and "throw ex" we can say "throw" maintains the full hierarchy in the stack trace and gives complete information about the exception occurred in the code. Whereas "throw ex" pretends that exceptions occurred on the line where "throw ex" was written and removes all the hierarchy above the method containing the "throw ex" expression.
I hope this article helped you in understanding the difference between "throw" and "throw ex".
Happy coding.


Anuja SarnaikPosted Jul 25, 2021, 7:31 AM EDT
Well explained
Dilshad AshrafPosted Aug 18, 2020, 8:55 AM EDT
In general program or any project, which one we should use, which one will be better for the real-time scenario. "throw" or "throw ex".
Dilshad AshrafPosted Aug 18, 2020, 8:52 AM EDT
Thanks for sharing..
Gunasekhar KotlaPosted Nov 16, 2017, 7:33 PM EST
Nice one sir.....Help full
Mazhar PashaPosted May 16, 2017, 4:30 AM EDT
Good one..Thanks for sharing...
Asfend YarPosted Feb 26, 2016, 7:15 AM EST
nice information
Jaipal ReddyPosted Jun 21, 2015, 8:45 PM EDT
good one
Nitesh KejriwalPosted Jun 19, 2015, 3:50 AM EDT
Welcome to community :)
Vipul MalhotraPosted Jun 18, 2015, 11:25 PM EDT
Thanks Akash for sharing it with us. Really helped
RakeshPosted Jun 18, 2015, 10:51 AM EDT
Good one
Santhakumar MunuswamyPosted Jun 18, 2015, 10:15 AM EDT
Thanks for nice one
Akash MalhotraPosted Jun 18, 2015, 5:42 AM EDT
Thanks...
Pankaj Kumar ChoudharyTOP 100Posted Jun 18, 2015, 4:22 AM EDT
Nice Article sir..........
Sibeesh VenuTOP 100Posted Jun 18, 2015, 2:00 AM EDT
Good one.
Kannan SudhakaranPosted Jun 17, 2015, 11:22 PM EDT
Nice
Khan Abrar AhmedPosted Jun 17, 2015, 11:04 PM EDT
Nice information, this will help.