Good morning! Good afternoon! Good Evening! from whichever location you are watching this video.
Today we are going to talk about the difference between Throw and Throw ex in C#.
On stack overflow I found this question out there "Is there a difference between
throw and throw ex?" and found lot of answers down below and all the answers almost are right
but what I though about is how about demonstrating it with a small sample
code because once you see a demonstration it just gets registered into your head.
This is one of the questions which keeps popping up in C# interviews. During C# interviews I keep
seeing this question so as a developer I've just used always throw. I never cared about throw ex
or throw but by default I always throw. In this 6 minutes of video we will try to understand what
is the difference between throw and throw ex? Also at the end I will talk about why
in certain conditions we have to reset the stack trace of the error.
Let us get started, here is the simple console application I've and in this
application Fun1 & Fun2. Assume that I make a call from main to fun1 and I make a call from
Fun1 to call Fun2. From static void main I am calling Fun1 and Fun1 is calling Fun2 and let
us say in Fun2 we have an exception. Now that we have an error in Fun2 we need to put a try catch
blocking Fun1. I am going to use code snippet, we have a ready made cod snippet for try catch.
I will wrap this Fun2 inside this try block out here. Fun1 is also throwing up this exception
to the main. Here I will put a try catch block and put fun1 out here. Fun2 throws up an exception it
goes to Fun1, Fun1 throws up an exception and it goes to the static void main. We have like
three functions Fun2 throwing up an exception to Fun1, Fun1 rethrowing that exception to static
void main. In the static void main I will display Console.WriteLine the strack trace. I will go into
the complete trace from where the error happened. The orgination of error is from Fun2
then it goes to Fun1 and then it goes to static void main. I would like to know
the complete stack trace of where the error is. If I run this, do a f5 out here and see
it very clearly showing the complete stack. It says on line no. 37
in program.cs the fun2 threw up an exception then it was sent to Fun1 which is at 26 and then it was
sent to static void main and so on. I get a complete stack trace of my error.
It maintains a complete stack trace of the error when I use throw.
I repeat the sentence, it maintains a complete stack trace form where the error orginated
and the whole stack till where it went it maintains a complete path of the stack trace
of I use throw. If I use throw ex --> do f5 and if we see the output
you would come to know the difference now. It is not showing Fun2
the throw ex resets the stack trace while the throw maintains the stack trace.
Throw will maintain the who was the orginator from where the error was happened but throw ex
resets the stack trace. It is always a good habit to use just throw because
when you have an exception you would like to know where the error was it originated
and how it propogated and so on. Whenever you are using the code snippet like try catch
by default Microsoft follows good practices and it just says throw it does not say throw ex.
The difference between throw and throw ex is that Throw maintains the stack trace
while Throw ex resets the stack trace. The next question which can come to your mind is
why would I not want to see my stack trace? As a developer we would love to see the stack trace,
from where it was originated and so on. But lot of times we do not want to expose out our internal
working of our library. For example, let us say end user sitting on your screen and sees the
complete stack trace of and functions how does it look like? You must have come across the websites
where you see those exception and say look at that line no. 21 I know that exception. On a public
facing website or on the enduser UI it would look very horrible to show the complete stack trace.
Many times your libraries used by some third party you don't want to expose out your internal working
of your library. In those cases you would like to just use throw ex and reset your stack trace.
I hopw that you enjoyed this video, in this video we were trying to understand what is
the difference between throw and throw ex? One resets the stack trace another
propogates the stack trace till the end. Thank You!