1 00:00:06,840 --> 00:00:14,730 So in this video, we're going to talk about Question 14, one of the most common questions during C-sharp 2 00:00:14,730 --> 00:00:15,540 interviews. 3 00:00:16,080 --> 00:00:21,660 What is the difference between Troll and Troll X when catching an exception? 4 00:00:21,780 --> 00:00:26,100 We don't always wants to handle it and read the program execution continue. 5 00:00:26,430 --> 00:00:32,130 Sometimes we want the exception to move on and perhaps be caught in the next cut growth. 6 00:00:32,400 --> 00:00:38,340 But we want to look something or do another action related to this exception occurrence. 7 00:00:38,790 --> 00:00:41,820 Such a field is called exception redrawing. 8 00:00:42,090 --> 00:00:50,100 We can do it by either using Troll Auto X. The difference is that through preserves the stack trace, 9 00:00:50,310 --> 00:00:55,740 so the Stack Trace will point to the method that caused the exception in the first place. 10 00:00:55,950 --> 00:01:04,230 While Strokes does not preserve the stack trace, it means we will lose the information about the method 11 00:01:04,260 --> 00:01:06,420 that caused the exception in the first place. 12 00:01:06,810 --> 00:01:12,240 It will seem like the exception was drawn from the place of its cutting and redrawing. 13 00:01:12,830 --> 00:01:19,470 We will see this in the court in a second, but first, let's be sure we understand what the stack traces. 14 00:01:19,950 --> 00:01:26,340 The stack trace is a trace of all methods that have been called that led to a particular moment in the 15 00:01:26,350 --> 00:01:26,940 code. 16 00:01:27,210 --> 00:01:29,370 Let's consider the following code. 17 00:01:30,550 --> 00:01:38,530 As you can see, method calls method we would call Smith at sea in the Method C will log the current 18 00:01:38,530 --> 00:01:40,150 state of this Darktrace. 19 00:01:40,570 --> 00:01:46,810 Let's see what will be printed to the council if we call metadata from the main method of the program. 20 00:01:49,680 --> 00:01:54,610 At the top of the stack trace, we have the method that has been called most recently. 21 00:01:54,840 --> 00:02:01,800 And at the bottom, the one that has been called first, the Stack Trace stores the information about 22 00:02:01,800 --> 00:02:06,420 automated calls that led to the current moment in the program execution. 23 00:02:06,960 --> 00:02:14,070 As you can see, the glitter of the environment stack trace property is at the very top because we read 24 00:02:14,070 --> 00:02:15,700 the Stack Trace, which is this method. 25 00:02:15,720 --> 00:02:16,470 Exactly. 26 00:02:16,770 --> 00:02:18,810 So it's the latest to be called. 27 00:02:19,410 --> 00:02:26,790 Please note that also the numbers of the lines of code where those methods have been called are stored 28 00:02:26,790 --> 00:02:30,360 in this Darktrace Stack Trace has many uses. 29 00:02:30,780 --> 00:02:38,400 But for us as developers, the most important value it brings is that it helps us track were some exceptions 30 00:02:38,400 --> 00:02:39,000 happened. 31 00:02:39,390 --> 00:02:45,870 Imagine that you have a huge up and when it throws an exception, oh, it says object is no. 32 00:02:46,470 --> 00:02:48,330 Well, that wouldn't be very helpful. 33 00:02:48,780 --> 00:02:54,780 We want to know the exact method that caused the problem and even more the exact line. 34 00:02:55,350 --> 00:02:58,170 This will help us to take a look at the right place. 35 00:02:58,410 --> 00:03:03,390 Put a breakpoint there and overall solve the problem easier and faster. 36 00:03:04,380 --> 00:03:05,130 All right. 37 00:03:05,520 --> 00:03:13,260 We said that True preserves the stack trace, well, Joe X doesn't want it is sometimes called resetting 38 00:03:13,260 --> 00:03:14,250 this Darktrace. 39 00:03:14,730 --> 00:03:17,580 Let's see the code that will show us the difference. 40 00:03:18,150 --> 00:03:19,620 Here is the first method. 41 00:03:19,980 --> 00:03:25,820 As you can see, it's designed to throw an exception because it will try to access the first number 42 00:03:25,890 --> 00:03:27,180 in an empty collection. 43 00:03:28,230 --> 00:03:30,180 And here is the second method. 44 00:03:30,360 --> 00:03:34,230 Almost the same, but doing through X instead of throw. 45 00:03:35,430 --> 00:03:41,490 As you can see, Visual Studio underlines throw eggs, and it has good reasons to do so. 46 00:03:41,800 --> 00:03:43,860 We'll go back to it in a while. 47 00:03:44,040 --> 00:03:48,630 First, let's see those methods in use for both of them. 48 00:03:48,780 --> 00:03:52,230 I called them in the try book in the catalog. 49 00:03:52,380 --> 00:03:56,070 I'm simply writing the stack trace stored in the exception. 50 00:03:56,820 --> 00:03:59,610 And now let's see the output of the program. 51 00:04:02,940 --> 00:04:09,660 For all the stack, trace and isolate links to her power, the previous entry serves about the first 52 00:04:09,660 --> 00:04:17,070 method, which already gives us some information about the nature of the problem for Trail X, the Stack 53 00:04:17,070 --> 00:04:20,910 Trace and admitted Troll X Line 52. 54 00:04:23,120 --> 00:04:24,030 What is this one? 55 00:04:24,050 --> 00:04:24,800 Exactly? 56 00:04:25,250 --> 00:04:31,820 This means that all information that has been stored in the stack trace before reaching the troll comment 57 00:04:31,910 --> 00:04:32,720 is lost. 58 00:04:33,170 --> 00:04:38,870 This is not good for us as we lose valuable data about the origins of the exception. 59 00:04:39,470 --> 00:04:46,490 This is why here we see Visual Studio suggesting to us that using Troll X is not a very good idea. 60 00:04:46,940 --> 00:04:50,540 We should stick to using Troll, not through works. 61 00:04:51,620 --> 00:04:56,600 One may wonder why is there a possibility to do it in C-sharp at all? 62 00:04:56,840 --> 00:04:58,550 If we should not ever use it? 63 00:04:58,940 --> 00:05:02,710 Well, remember that this X is just an object. 64 00:05:02,720 --> 00:05:04,040 Although exception grass. 65 00:05:04,640 --> 00:05:10,940 We throw objects belonging to this class all the time, and it's perfectly fine only that we troll brand 66 00:05:10,940 --> 00:05:14,630 new exceptions, not the ones that have already been thrown. 67 00:05:16,240 --> 00:05:23,410 If C-sharp allows me to do this with the argument exception object, it must allow me to do this with 68 00:05:23,410 --> 00:05:25,930 this object, which is also an exception. 69 00:05:26,530 --> 00:05:32,260 The compiler can truly prevent us from doing so, even if it's not such a good idea. 70 00:05:33,010 --> 00:05:40,540 Let's summarize the difference between Troll and Troll X is that troll preserves the stack trace while 71 00:05:40,690 --> 00:05:47,920 or X does not do that with the Troll X will lose the information about the origins of the exception, 72 00:05:48,280 --> 00:05:54,100 and instead, it will seem like the exception was strong from the place of its cutting and trolling. 73 00:05:54,940 --> 00:05:59,620 Questions related to this topic that you may feel during the interview could be. 74 00:05:59,860 --> 00:06:01,240 What is the Stack Trace? 75 00:06:01,690 --> 00:06:07,480 The Stack Trace is a trace of all methods that have been called, and that led to the current moment 76 00:06:07,480 --> 00:06:10,630 of execution at the top of the Stack Trace. 77 00:06:10,840 --> 00:06:14,920 We have the method that has been called most recently and at the bottom. 78 00:06:15,130 --> 00:06:22,030 The one that has been called first stack trace allows us to locate the exact line in the code that was 79 00:06:22,030 --> 00:06:27,330 the source of an exception should be a strong or true X and Y. 80 00:06:27,910 --> 00:06:34,270 We should use Troll as it preserved the stack trace and helps us find the original source of the problem. 81 00:06:35,230 --> 00:06:38,770 All right, that's it about the topic of exceptions. 82 00:06:39,370 --> 00:06:42,760 Thanks for following along and see you in the next video.