1

00:00:01,140  -->  00:00:07,180
Not let's look at something Goddard finally broke we have seen how exhibitions can be generated on how

2

00:00:07,180  -->  00:00:08,270
they can behind it.

3

00:00:09,110  -->  00:00:15,690
We also seeing how exceptions can affect the control for itself that is assoon as an exception is genderqueer

4

00:00:16,140  -->  00:00:22,960
rest of the block is squibbed and GBM stops looking for matching exception handler not really such a

5

00:00:23,010  -->  00:00:24,210
grow is fine.

6

00:00:24,550  -->  00:00:29,730
But sometimes the board might be using some operating system resources in such cases.

7

00:00:29,770  -->  00:00:37,760
You would have to become extra care for instance here is I want a sun method from them all for the sake

8

00:00:37,760  -->  00:00:38,920
of this discussion.

9

00:00:39,020  -->  00:00:44,490
The great catch block is movement with this method from the shared method also some of the code has

10

00:00:44,490  -->  00:00:46,170
been omitted.

11

00:00:46,180  -->  00:00:50,580
Now if you recall if distintion barometer has a value of zero.

12

00:00:50,940  -->  00:00:53,860
We will read the input data are we a local file.

13

00:00:54,230  -->  00:01:01,290
And we can use a file open stream class for the final stream would use operating system resources to

14

00:01:01,290  -->  00:01:02,800
connect to their destination.

15

00:01:03,850  -->  00:01:11,760
These system resources once used to be Glos appropriately to avoid any resource leaks this a medical

16

00:01:11,830  -->  00:01:19,060
Glos is in work on the final stream object so the question is valid will replace the last invocation

17

00:01:19,100  -->  00:01:22,180
statement so that it gets executed for sure.

18

00:01:23,940  -->  00:01:30,730
For instance you may and so did at the end of the play Brock but we know that in this case if an exception

19

00:01:30,730  -->  00:01:37,660
is generated let's say when creating a file or part stream object the rest of the try block is on JVM

20

00:01:37,660  -->  00:01:44,170
starts looking for a matching exception handler since the block is Glos method will not be invoked in

21

00:01:44,170  -->  00:01:47,230
this case.

22

00:01:47,370  -->  00:01:50,630
Another option is to place it at the end of the matter.

23

00:01:51,090  -->  00:01:57,840
But the problem with that is if an unchecked exception is generated due to whatever reason then it will

24

00:01:57,840  -->  00:02:04,380
not be caught by either of these to catch blocks uncontrolled and switched to the calling method unclose

25

00:02:04,380  -->  00:02:12,770
method will once again be knocking more among other options you ordered in the first guy you block.

26

00:02:13,310  -->  00:02:19,490
But if an exception is generated instead of why not one exception then controlling what goes into this

27

00:02:19,490  -->  00:02:20,550
first catch block.

28

00:02:20,660  -->  00:02:27,130
And once again grows is not in book know in addition to the first Gutch groc if you also in-circuit

29

00:02:27,140  -->  00:02:34,110
in the second catch block then that may still not help if some unchecked exception is generated even

30

00:02:34,110  -->  00:02:38,090
if that's not book is The Gordy's cluttered with multiple close calls.

31

00:02:39,170  -->  00:02:44,970
So what we need is love grows in vocations statement and it needs to be executed for sure.

32

00:02:45,440  -->  00:02:52,180
So we need a got into either an that we can use a finally block in this me.

33

00:02:52,690  -->  00:02:57,090
It's an optional block an event present at all residents.

34

00:02:57,120  -->  00:03:02,950
It would run even if an exception is generated and the corresponding exception handler is not present

35

00:03:02,950  -->  00:03:03,770
in this matter.

36

00:03:04,720  -->  00:03:12,400
Only exception would be when GBM engenders a severe error or a medical exit from the system class is

37

00:03:12,400  -->  00:03:13,530
invoked.

38

00:03:13,780  -->  00:03:19,990
Exit mentor is used to determine the JVM itself which should be done in only some really special cases

39

00:03:19,990  -->  00:03:20,200
.

40

00:03:21,610  -->  00:03:27,180
As we see in this example finally block usually condense any cleanup or.

41

00:03:27,260  -->  00:03:34,730
Another example would be closing of is going to Currence So if you have to do something for sure regardless

42

00:03:34,730  -->  00:03:40,460
of whether or not an exception is generated then you would use a fine liberal otherwise you wouldn't

43

00:03:40,460  -->  00:03:41,110
need it.

44

00:03:43,130  -->  00:03:49,740
Just know that finally block must follow any Gottsch blocks it cannot come in between a cry unaccountably

45

00:03:49,750  -->  00:03:57,710
up or in between Bukvich blocks but if not catch blocks exist then it must follow that try block so

46

00:03:57,710  -->  00:04:03,950
what that means is a dry rock must be accompanied by at least one catch block or finally broke.

47

00:04:05,230  -->  00:04:09,670
Also you can opt out more than one finally block associated with the try block.

48

00:04:11,300  -->  00:04:16,740
Not let's look at the control flow when a finally block is in mind letters you'll see how one or more

49

00:04:16,740  -->  00:04:22,800
statements that may generate exceptions and these statements are enclosed in their Tricot finally broke

50

00:04:22,800  -->  00:04:23,640
.

51

00:04:23,640  -->  00:04:27,220
So let's look at the cases when an exception is non-gender get.

52

00:04:27,330  -->  00:04:28,650
Also when it is genetic.

53

00:04:30,710  -->  00:04:34,870
Krusty is umed that no exception is generated then this is what would happen.

54

00:04:36,000  -->  00:04:41,410
Try block will be executed completely which will be followed by the finally block which is then followed

55

00:04:41,410  -->  00:04:45,800
by the code that comes after the finally block.

56

00:04:46,070  -->  00:04:52,370
But if an exception is generated then the rest of the block is skipped and GBM will check if there is

57

00:04:52,370  -->  00:04:59,460
a matching exception handler within that method and if there is a matching exception handler then that

58

00:04:59,460  -->  00:05:04,920
particular catch block is executed which will be followed by the finally block which is then followed

59

00:05:04,920  -->  00:05:09,700
by any code that comes after the final E-block.

60

00:05:09,940  -->  00:05:15,140
But if a matching exception handler is not found then the finally block is executed.

61

00:05:15,350  -->  00:05:21,820
But the important thing is in ecord following the finally block is gipped on control which is over to

62

00:05:21,840  -->  00:05:23,460
the working mother.

63

00:05:23,840  -->  00:05:27,890
And this process is repeated until a matching exception handler is fun.

64

00:05:28,400  -->  00:05:34,530
For example if we assume that the invoking meant that also includes a try catch finally block then the

65

00:05:34,540  -->  00:05:36,050
rest of the cries give.

66

00:05:36,230  -->  00:05:43,100
And if unmatching exception handler is not fun once again then the finally is executed but not any code

67

00:05:43,100  -->  00:05:44,120
following it.

68

00:05:44,600  -->  00:05:49,170
So that keeps repeating until the matching exception handler is phone.

69

00:05:49,330  -->  00:05:56,170
So the point here is if an exception is generated then you cannot exit to any court other than the court

70

00:05:56,170  -->  00:05:58,030
in a matching exception handler.

71

00:05:58,090  -->  00:06:04,540
And the finally block so that's above the control flow and it's not just this different control focuses

72

00:06:04,630  -->  00:06:05,610
in court.

73

00:06:06,010  -->  00:06:13,650
So let's switch over to equites OK here we are in the exceptions to mock glass in the sheer amount that

74

00:06:14,310  -->  00:06:20,840
we just discussed three control freak cases and we're going to test all three of them so we will at

75

00:06:20,880  -->  00:06:27,090
the finally block within this shared method are not in the sun method and the history behind us.

76

00:06:27,270  -->  00:06:28,600
So let's just go ahead.

77

00:06:28,650  -->  00:06:34,890
Cried the first case which was when an exception is not generated but in other words an exception is

78

00:06:34,890  -->  00:06:35,820
always Doniger.

79

00:06:35,850  -->  00:06:38,220
So we need to make some minor changes.

80

00:06:38,230  -->  00:06:40,280
First thing is let's pass one here.

81

00:06:40,910  -->  00:06:46,890
And if you pass one here or destination would be one in an Iowa exception will be generated.

82

00:06:46,950  -->  00:06:52,980
But since we don't want an exception to be gender so let's just call him and just out and go back here

83

00:06:52,990  -->  00:06:53,260
.

84

00:06:54,800  -->  00:07:00,610
And that's put in print statement here let's say after working seven which is good.

85

00:07:00,940  -->  00:07:04,130
And that's also how I finally block here.

86

00:07:12,360  -->  00:07:14,380
Inside she is finally.

87

00:07:14,520  -->  00:07:16,400
So let's just go ahead and run this

88

00:07:19,580  -->  00:07:21,950
so as you can see we are going into.

89

00:07:22,020  -->  00:07:28,940
There are no exception is thrown so sentiment that executes completely without any shoes and after dark

90

00:07:29,480  -->  00:07:36,110
after invoking sense statement as presented here an afterthought after the try block is executed completely

91

00:07:36,650  -->  00:07:39,070
and after that the finally block is executed.

92

00:07:39,140  -->  00:07:40,450
So it's been doing this.

93

00:07:40,790  -->  00:07:45,750
And finally the court after finally block is executed here and Afshar.

94

00:07:46,030  -->  00:07:47,010
And we are good with that.

95

00:07:47,090  -->  00:07:52,490
So that's the first case and second case was when an exception is generated and a matching exception

96

00:07:52,490  -->  00:07:53,690
handler is fun.

97

00:07:53,810  -->  00:08:00,980
So let's just uncommon this so an exception is generated and we have a matching exception handler here

98

00:08:00,990  -->  00:08:01,110
.

99

00:08:01,220  -->  00:08:01,570
OK.

100

00:08:01,640  -->  00:08:04,540
So let's just simply exhibit this.

101

00:08:04,550  -->  00:08:10,480
So now we're going into insights and then exception is gender Witan doesn't matter.

102

00:08:10,490  -->  00:08:16,920
So this statement so the rest of the trial escaped and the catch block is executed.

103

00:08:17,060  -->  00:08:23,240
So it Brent's kind of thing different so we're on after the catches executer finally is executed here

104

00:08:23,590  -->  00:08:28,500
and after finally the court after finally is executed which is not enough sure.

105

00:08:28,610  -->  00:08:29,240
OK.

106

00:08:29,240  -->  00:08:30,600
So that's the second case.

107

00:08:30,710  -->  00:08:37,810
And third one is the slightly complex case or more interesting case which is an exception is gender

108

00:08:37,940  -->  00:08:42,150
but a matching exception handler is not found in the matter.

109

00:08:42,410  -->  00:08:45,190
So let's just go ahead and pass zero here.

110

00:08:46,610  -->  00:08:50,650
And here for zero I find not one exception is generated.

111

00:08:50,840  -->  00:08:57,530
So undrunk comes back here but in the case we've discussed there is no matching exception handler within

112

00:08:57,530  -->  00:08:58,520
this method.

113

00:08:58,520  -->  00:09:02,610
But in this case we have a handler but we are still retrying the exception.

114

00:09:02,720  -->  00:09:04,390
So the control would go here.

115

00:09:04,790  -->  00:09:09,260
But then but then we are also throwing it retrying the exception.

116

00:09:09,260  -->  00:09:12,120
So it's a slightly more interesting case actually.

117

00:09:12,200  -->  00:09:13,280
So we are redrawing it.

118

00:09:13,280  -->  00:09:17,950
So in that case with the finally block we'll also the catch block will be exempted or defined and we

119

00:09:17,950  -->  00:09:19,120
will be executor.

120

00:09:19,280  -->  00:09:23,490
But the court after finally will not be exaggerated as we are reviewing it.

121

00:09:23,720  -->  00:09:28,840
So the GBM would look in the main method which is actually invoking the Sheremet there.

122

00:09:29,360  -->  00:09:31,090
So let's actually go ahead and try this.

123

00:09:31,080  -->  00:09:32,220
So it's zero.

124

00:09:32,660  -->  00:09:45,360
Let's put a print statement here also let's just say share Kallen why not for an exhibition.

125

00:09:45,370  -->  00:09:46,820
So that's good enough here.

126

00:09:47,320  -->  00:09:53,870
And we are throwing an exception to do during an exception there are let's just uncommon this out

127

00:09:57,420  -->  00:10:09,680
and let's put statement here after invoking share that meeting Goleman desart so they can grow together

128

00:10:09,730  -->  00:10:09,840
.

129

00:10:09,890  -->  00:10:12,580
Also come here in this Gottsch block.

130

00:10:12,740  -->  00:10:20,160
So let's just say Maine why not for an exception.

131

00:10:20,310  -->  00:10:22,850
And let's also put a maned finally here.

132

00:10:34,030  -->  00:10:35,190
Students execute that.

133

00:10:35,200  -->  00:10:39,960
So we are going into inside saying densify not one exception is generated.

134

00:10:40,000  -->  00:10:47,080
So can comes here and do the catch block in share bins this message here share Dunder inside shares

135

00:10:47,080  -->  00:10:53,600
finally is executed but important thing is if shared is not printed here ironed.

136

00:10:53,890  -->  00:11:02,290
So the controller goes back into the main matter on inside the main method there is a there is a catch

137

00:11:02,290  -->  00:11:05,430
block here matching Gutch block so it prints this message here.

138

00:11:05,440  -->  00:11:07,430
Main find not one exception.

139

00:11:07,450  -->  00:11:13,660
Then it goes into my inside Mayne's finally Anson's a catch block has already been encountered here

140

00:11:13,680  -->  00:11:13,920
.

141

00:11:14,200  -->  00:11:16,230
It burns and off main mentored.

142

00:11:16,410  -->  00:11:20,510
OK but if we are doing here then this message will not be printed.

143

00:11:20,530  -->  00:11:24,420
So after INSIDE Mayne's finally the program would actually dominate.

144

00:11:24,700  -->  00:11:27,630
OK so that's the only important difference.

145

00:11:27,640  -->  00:11:30,360
This particular statement will not be printed.

146

00:11:30,430  -->  00:11:37,660
So basically when an exception is generated no code will be executed except for unmatching got a matching

147

00:11:37,660  -->  00:11:40,870
exception handler block undefined on the block.

148

00:11:40,870  -->  00:11:41,270
All right.

149

00:11:41,280  -->  00:11:42,140
This goes on.

150

00:11:42,160  -->  00:11:45,280
So that's the third case and that's about it.

151

00:11:45,280  -->  00:11:51,220
Please go ahead and try this out and you can also find the good programs in the resources section.

152

00:11:51,290  -->  00:11:51,820
Thank you.

153

00:11:51,820  -->  00:11:52,710
And happy coding.
