So in this video, we're going to talk about Question 14, one of the most common questions during C-sharp
interviews.
What is the difference between Troll and Troll X when catching an exception?
We don't always wants to handle it and read the program execution continue.
Sometimes we want the exception to move on and perhaps be caught in the next cut growth.
But we want to look something or do another action related to this exception occurrence.
Such a field is called exception redrawing.
We can do it by either using Troll Auto X. The difference is that through preserves the stack trace,
so the Stack Trace will point to the method that caused the exception in the first place.
While Strokes does not preserve the stack trace, it means we will lose the information about the method
that caused the exception in the first place.
It will seem like the exception was drawn from the place of its cutting and redrawing.
We will see this in the court in a second, but first, let's be sure we understand what the stack traces.
The stack trace is a trace of all methods that have been called that led to a particular moment in the
code.
Let's consider the following code.
As you can see, method calls method we would call Smith at sea in the Method C will log the current
state of this Darktrace.
Let's see what will be printed to the council if we call metadata from the main method of the program.
At the top of the stack trace, we have the method that has been called most recently.
And at the bottom, the one that has been called first, the Stack Trace stores the information about
automated calls that led to the current moment in the program execution.
As you can see, the glitter of the environment stack trace property is at the very top because we read
the Stack Trace, which is this method.
Exactly.
So it's the latest to be called.
Please note that also the numbers of the lines of code where those methods have been called are stored
in this Darktrace Stack Trace has many uses.
But for us as developers, the most important value it brings is that it helps us track were some exceptions
happened.
Imagine that you have a huge up and when it throws an exception, oh, it says object is no.
Well, that wouldn't be very helpful.
We want to know the exact method that caused the problem and even more the exact line.
This will help us to take a look at the right place.
Put a breakpoint there and overall solve the problem easier and faster.
All right.
We said that True preserves the stack trace, well, Joe X doesn't want it is sometimes called resetting
this Darktrace.
Let's see the code that will show us the difference.
Here is the first method.
As you can see, it's designed to throw an exception because it will try to access the first number
in an empty collection.
And here is the second method.
Almost the same, but doing through X instead of throw.
As you can see, Visual Studio underlines throw eggs, and it has good reasons to do so.
We'll go back to it in a while.
First, let's see those methods in use for both of them.
I called them in the try book in the catalog.
I'm simply writing the stack trace stored in the exception.
And now let's see the output of the program.
For all the stack, trace and isolate links to her power, the previous entry serves about the first
method, which already gives us some information about the nature of the problem for Trail X, the Stack
Trace and admitted Troll X Line 52.
What is this one?
Exactly?
This means that all information that has been stored in the stack trace before reaching the troll comment
is lost.
This is not good for us as we lose valuable data about the origins of the exception.
This is why here we see Visual Studio suggesting to us that using Troll X is not a very good idea.
We should stick to using Troll, not through works.
One may wonder why is there a possibility to do it in C-sharp at all?
If we should not ever use it?
Well, remember that this X is just an object.
Although exception grass.
We throw objects belonging to this class all the time, and it's perfectly fine only that we troll brand
new exceptions, not the ones that have already been thrown.
If C-sharp allows me to do this with the argument exception object, it must allow me to do this with
this object, which is also an exception.
The compiler can truly prevent us from doing so, even if it's not such a good idea.
Let's summarize the difference between Troll and Troll X is that troll preserves the stack trace while
or X does not do that with the Troll X will lose the information about the origins of the exception,
and instead, it will seem like the exception was strong from the place of its cutting and trolling.
Questions related to this topic that you may feel during the interview could be.
What is the Stack Trace?
The Stack Trace is a trace of all methods that have been called, and that led to the current moment
of execution at the top of the Stack Trace.
We have the method that has been called most recently and at the bottom.
The one that has been called first stack trace allows us to locate the exact line in the code that was
the source of an exception should be a strong or true X and Y.
We should use Troll as it preserved the stack trace and helps us find the original source of the problem.
All right, that's it about the topic of exceptions.
Thanks for following along and see you in the next video.