Let's begin by first learning about how exceptions are represented on how they can be hundred. 2
An exception is simply an object of the class through habit or one off it's up process. 3
So to simply an object like any other good job object on Trouble is from the outline package under the 4
superclass of all exceptionally literate classes. 5
And as I mentioned earlier an exception indicates an exceptional situation like a partner solver going 6
on in such a case a method or a constructor gender's the exception. 7
Now let's head over to Eclipse and we will learn about exceptions by directory writing some code. 8
I've created a new package called com not semantics. 9
There are exceptions within the demo project and within the exceptions package classes have been created 10
one has exceptions them all and other is hitcher to be connected and within the exceptions class there 11
are two amateur's. 12
One is mine and the other is share on the share that is very similar with a shared method that we have 13
seen in the in the earlier project. 14
So if you recall in Threepio project the sheer method was used to share a bookmark with a fictitious 15
partner Web site called good snips. 16
And so the shared method is also very similar to that and within the history to be connected with us 17
. 18
We have this method called Center and the main method actually invokes shared motard and shared method 19
in done in Wilkes. 20
The sentiment that it's a static method and has GTP connect Ansen method actually does that are real 21
sharing of data of it with snips. 22
OK so even though share is supposed to do the sharing. 23
But the actual sharing will be done by send and it has all the low level details for shading off the 24
end of the with the partner Web site. 25
OK. 26
So that's the assignment. 27
So we have just three template metrics in each method. 28
We have to print statements one or the top one at the end at the top it says in site for example here 29
it says inside me are not the end users end of Main. 30
Similarly in shared how insights share on and off each year on scene with the Send method also and these 31
print statements have been included in order to look at the control flow. 32
So the control flow defers based on whether or not an exception is being has been generated. 33
If an exception is generated then you would how one type of water flow. 34
But if the next option is non-gendered then you will see a different control flow. 35
So in order to keep track of dock we have these print statements. 36
And so let's just go ahead and start filling up these methods. 37
Let's begin with the sand method and it has three parameters one in parameter on string parameters. 38
So here the data is one of the parameters and the goal of this method is to send this date talked with 39
a partner up site and destination retailers how the it is going to be transmitted. 40
So we have two ways in which the data can be shared. 41
So the first way if destination is zero it means that there will be written to a file on the local Sarber 42
on the harness and then there'll be some kind of a script which will push their data to the partner 43
Web site. 44
So that's one way. 45
And if destination is one it means that the Send method will programmatically push their data directly 46
to the partners partner to the partner site and really push it to its webservices let's say the part 47
of several host of absurdest ironed destination is one Sandwell directory pushed their data to the partners 48
absurdest. 49
OK so those are the two ways in which they can be shared with the partner Web site and exhibitor domain 50
but the value their destination has. 51
So let's begin and let's just assume that destination is one. 52
So in this lesson we will look at only when data is written to the file and through that process we 53
will learn something about how exceptions are generated are and how that can be handled in the next 54
lesson. 55
We will also look at the second approach. 56
One destination is to one destination is one and we will try to learn a few more concepts but we not 57
going to do any We are not going to write any code for shading off the data like writing good a file 58
are calling the passing grade of service that we are not going to do so because that's that's not in 59
the context here. 60
So you know our goal is to just learn about exceptions and be I just use using this example as a use 61
case. 62
OK. 63
So let's just go ahead and read this here now when we are if we have to write somebody to a file we 64
use a class called file output stream which is from the dollar package and is responsible for writing 65
the report with a fine. 66
Now if something goes wrong when writing the data then an exception will be generated by the file output 67
stream class. 68
OK but the exception will be generated by that particular class. 69
So if you just use that class you're not going to learn about how an exception can be generated. 70
Correct. 71
So the goal for us is to see how we can generate an exception because we are still learning a lot about 72
the exception generation process. 73
So let's just assume that we are trying to write the data file and somehow the file is missing. 74
So we need to generate an exception. 75
So that's an exceptional situation. 76
We say that exceptions need to be generated only an exceptional situations. 77
So in this case the exceptional situation is that there is no file and the file is actually missing 78
and we cannot write that out with that particular form. 79
So that's an exceptional situation. 80
So in this case we need to generate an error and an exception. 81
So for that we use something called a troll statement and it starts with a rookie. 82
And we said that an exception is basically an object of the class through mobile or one off it's subprocess 83
. 84
OK. 85
In this case B the subclass is called as a file not found exception and that's the exact class that's 86
also used by the file output stream class. 87
OK so if there is any error it generates the file not found exception. 88
So let's just use that class on gender to an instance of doc of that particular class 89
and it's giving a compiler error because we need to import it and it belongs to the IO package. 90
OK. 91
And it is a subclass off trouble and trouble is the superclass of all off all exceptions. 92
But it still gives a compiler error. 93
And that's because this particular method is generating this exception and there is some other code 94
which is invoked which will be been working this matter and that in working code needs to know about 95
this particular exception. 96
OK so here in this case this exception is being generated within the body of the Send method. 97
So it's an implementation detail but how will the in working code know about this particular exception 98
. 99
It needs to somehow know about it. 100
Right. 101
And for that we need to indicate that this particular method might throw this exception and that we 102
need to indicate that here in the in the in the method declaration itself. 103
So if I hover over it it gives me two options as one handle exception type unhandled exception type 104
either use surrounded with a quick catch but we will see later or add to this declaration. 105
And this is what we are going to do not now. 106
So it I it has added this exception to the declaration and it uses this keyword called Astro's. 107
OK Andreas can be followed by one or more exceptions. 108
OK. 109
So there is no more compiler compilation error. 110
So in all this and motard is saying that I might throw this exception also. 111
OK so that's what it is informing. 112
Any indication. 113
But if you go back to exceptions demobilize from the sheer method we are invoking the Send method and 114
this NABC a completion error here also. 115
And that's because the Send method has to handle this particular exception. 116
Now if I have got over it we again see the same options here. 117
It says either our throws declaration or surrounded with a try catch. 118
OK this time let's pick this up and once we look at that we will look at the first option also. 119
When I do that we have a try catch block here. 120
It's also called as an exception handler and the Send method is engrossed in the try block. 121
And there is a catch block here on the catch block has a parameter here called filing not found exception 122
. 123
OK. 124
So as you can see it looks very similar to we are calling it as a parameter. 125
So in that sense it is very similar to a matter. 126
All right. 127
You can't know within the. 128
This is the body of the catch block. 129
And here it's invoking a method called blenched daiquiris. 130
OK. 131
So this is the exception handler. 132
So if you want to write any recovery code when an exception is generated You need to write it here. 133
So basically an exception will be generated in the Send method and that exception will be handled here 134
. 135
So let's just go ahead and run this thing. 136
So as you can see if we initially were inside main then main is invoking share on Share is invoking 137
send. 138
And here you are passing zero. 139
So this needs to be zero here. 140
Let me just read on it. 141
Now you can see that within inside Senator. 142
So destination is zero. 143
And this statement is getting executed. 144
And as soon as this statement is gets executed the control goes back to the to the invoking method which 145
is share. 146
So the rest of the method is good to hear. 147
OK. 148
And this particular method and off center is not being rendered here only no share on enough meanness 149
here. 150
So the control goes back to the calling method. 151
So this method will be popped off the stack and then controller comes here. 152
And so then the exception is generated the exception of riposted the JVM and JVM and see if there is 153
an exception handling code within this method. 154
I know it's not there so. 155
So it pops off this method from the from the call stack and here it tries to see if there is an exception 156
handling code and there is an exception handler here. 157
So it just it just passes the exception object this particular pyramided it assigns it to this barometer 158
underpins the message. 159
And this is nothing but as doctoress basically does it gives information about the type of exception 160
here which is a file not found exception and it also gives her radius off the exception are sort of 161
created God generated initially on how that particular method got in. 162
For example here it says that the exception most generator that has strictly be connect at line pen 163
within the history class on that particular method was invoked from share method or here at line 17 164
. 165
That's very. 166
This meant that method was invoked and shared was again invoked by another method main. 167
OK so that's how you have done to try and catch block. 168
This is how you handle the mechanism. 169
This is how you handle exceptions and you would have some recovery code here. 170
OK. 171
So if an exception is generated it means it's generated in some exceptional situation in which case 172
we handle it. 173
And so you would have some recovery code here now. 174
So there is. 175
So you had this we thought this strike would be what we were getting a compilation error. 176
So the next step is if you are not handling it here and if we want to why compiler error then shared 177
method can actually can actually relate the error. 178
Which means it says that I'm not going to handle it much. 179
The Invoking method might be able to handle it. 180
So I'm just going to relate back with my main working mother. 181
OK so let's try that option also. 182
So in this case let's pick the first option as you can see it's the truest gloss has been added here 183
and the compiler is here. 184
So let's start on this share method with try catch block knowledge's just go ahead and look at the control 185
flow. 186
So you are inside. 187
Here you are inside us and then an exception has been generated. 188
And so this statement is not getting executed here Anderson method is bumped off the stack and control 189
comes back here and to the class and sees that the JVM sees that there is an exception handler here 190
. 191
So it's not going to even bring this statement and off share. 192
And I do not support the sharing method from the call stack and then it will go into the main method 193
and see if there is an X if there is a class here. 194
If there is an exception handler here and it is here it's going to print this thing. 195
And after the catch block then this statement will get executed and off Maine will get executed OK. 196
So you can see the control flow here. 197
Now so after the catch block is executed then the statements following the catch block will be executed 198
. 199
Now if he'd come and this out the only other thing or only other option as the main method would also 200
have to throw the exception. 201
But then in which case this statement will not get executed. 202
So in none of the statements will get executed because the the keep the JVM keeps popping off those 203
and those methods off the stock. 204
And finally it gets here it also pops this off and there is no other method to handle it. 205
I enjoy them also. 206
But you know it gives up and the program dominates. 207
Now if this is if this was a web application with hundreds of threads running simultaneously then one 208
third will be handling one user request and with in that case if such kind of an exception is generated 209
if it does not handle finally that that particular thread that belongs to that user will be terminated 210
. 211
OK. 212
So this is what we have here. 213
And and here one thing I can show is you can also have this and this particular gadget and actually 214
read toerag even this can be done. 215
So which means that control comes here. 216
But then this is going to read through it. 217
And then it needs to the main hospital still handle it OK or it needs to just you know relay it so you 218
can also catch block and also to an error. 219
So that's about it. 220
So we have looked at the exception generation on handling process and then the next lesson we will also 221
write the next block here for Destination equal to one and we'll try to explore a little bit more about 222
exceptions. 223
OK thank you. 224
And you can find the court and the resources section for it. 225
If the files do not how bargello extension and if to not be XDR or some other exception extension just 226
gone what did Kadar Java and you can compile and run it. 227
OK thank you. 228
See you in the next lesson. 229
Unhappy according