Let's not look at how we can create our own exceptions in the process we will also look at four very 2
important suggestions from effective giallo unheeded the four items that we are going to discuss and 3
discuss all these items include we will discuss the first two items in this lesson and the last two 4
will be discussed in the follow up lessons and the first one is item 58 and it says use checked exceptions 5
for recoverable conditions on runtime exceptions for programming it has to be under to discuss this 6
. 7
But I will discuss it in the context of creating new exception class itself. 8
Second one is straightforward underdosed items have to fly under sists not to ignore exceptions people 9
often by late date by using empty Gutch box which defeats the purpose of exceptions items is that ignoring 10
an exception is similar to ignoring a fire alarm third and fourth items will be discussed later. 11
And also supplemental notes will include a summary of all main points covering these items. 12
So let's go ahead and extend no exceptions Democritus 13
here we are in the Send method within the history class and we know that this method is responsible 14
for the actual shooting of the data with the partner. 15
And there are two ways to do it and we would be focusing on the second approach which is to pass the 16
data in a pragmatic way via web service. 17
And that happens if destination is equal to one and did a couple of genius to be made here and this 18
method one off them as the data type here. 19
Often there is a recurrent type has been changed from white to string. 20
And the reason for that is we just want it done the response that we get from the web service. 21
So the reps overstates the take somebody doesn't put a target and some response OK. 22
Like whether it's whether that data has been processed successfully or not. 23
And we just want to pretend that the doctor does to the working mother. 24
So here is one variable the variable which which is going to read and the value of it is initial estimates 25
success and it is really initialized here hear the response that we get from the web service. 26
Now we're not doing the actual web service interrogation so let's just assume that this is the response 27
that we got from the service. 28
OK. 29
So it's an example response. 30
And here is the root element which is it isn't and within that we have court and within court there 31
is this value I call success. 32
OK now let's assume that according to the API contract exhumed this was supposed to be a numeric value 33
Rexy all these days it was you know Mark uncertainly at this point of time when we made the invocation 34
did attend and non-numeric value string. 35
OK. 36
So basically they broke the API contract and this is done going to affect us as it really big our court 37
also. 38
So they shouldn't have done this but trust me in my experience with several teams usually most of the 39
time they don't. 40
Most of the teams that I work with they never change the API in this way. 41
But sometimes there were beams which did change the API that broke the cord that broke my cord. 42
So it can happen sometimes. 43
So that's when you're working with some unprofessional diems. 44
So you shouldn't be doing that. 45
So once the API is out you shouldn't change the response or even the request format. 46
So in case you want to change it you have to let your clients know in advance and or set two or three 47
weeks in advance so that they can make the necessary adjustments. 48
So if you're doing something you don't amend just don't change the API Midway. 49
Just make sure that your clients are informed in advance. 50
So here is their response and we are reading the response here. 51
Let's go back to exceptions to all this is a shared method from where we are in Woking. 52
So here is the response. 53
So it's getting assigned to this response variable. 54
A new class has been written. 55
API parser it's responsible for parsing the response on extracting individual data from it. 56
So it's a different component here and there is a new method within that. 57
It's Carlos parse send response code. 58
So it's going to take the response here as an in book. 59
It's going to extract the value of the code element. 60
OK. 61
So there wasn't anyone called court and it's going to extract that. 62
And you're also passing these two values. 63
Hello Aaron goodness which one also passed here to the Send method. 64
And literally will see why we need that. 65
OK. 66
So let's just going do this mattered here. 67
Here it is. 68
So this matter is going to return the actual response. 69
OK so it's going since it's supposed to be a numeric value. 70
So it's going to return. 71
And here so it's initialized or minus one. 72
And here is a card which is going to extract the the value off the code element. 73
OK. 74
So just don't worry about it you can look at it later. 75
But all it is doing is it is going to extract the code on in order to extract the code. 76
I mean it has it does extract extracting court but in order to convert it to and we are using the parse 77
and matured from the entire class. 78
Now this is going to generate an exception because the value is non-numeric right. 79
No because they have changed the API response but since we are a professional quarters we have a cricut 80
Sheir. 81
We want to because we want to handle it. 82
So let's bring it here it's printable you and it's going to give us an exception. 83
As you can see it says Number four marked exception because the input string is success. 84
So it has been broken not so now. 85
Let's create a new exception and throw that exception here so on later I will explain why. 86
Why do we need to create a new exception. 87
Why not simply propagate this exception so that will be disclosed in a later lesson which is also it 88
which basically is about one of the items that we are supposed to discuss. 89
So let's just go ahead and create a new exception since it's going to be an exception type of exception 90
. 91
So it has to extend one of the exception classes. 92
Right. 93
So let's just go ahead and do that since the API has changed. 94
Let's name the exception in a meaningful way and let's call it as API for might change exception. 95
Not we are going to look at a couple of suggestions from item 58 which is about you know would suggest 96
that we need to use checked exceptions if you want to recover from the exception or runtime exceptions 97
. 98
And if it isn't programming programming it or know we can do we can do an extra here. 99
Now we can also extend another. 100
But the item is that we should not extend or we should not subclass it. 101
And that's because there is a strong convention that errors should be generated only by JVM because 102
they are JVM related. 103
Under normal conditions. 104
So the items are just that we should not be doing that. 105
We should not be extending that ours. 106
So there's a strong convention for that if you want to you can do it but we shouldn't be doing it. 107
And that's that's the recommendation from the item now. 108
So if this is all good then we can do it on time exception. 109
That's one thing we can do. 110
In fact you might want to do that because this is a programming error. 111
The program is not written you know. 112
He hasn't doubled up this particular piece of art. 113
It's coming from an extra source but still you know it's kind of it's kind of a different component 114
even though it's a different satirise from an extra partner you can think of it as a different component 115
. 116
And so it's basically you have to be a programming error from though from somebody in the partner site 117
so it can be an exception. 118
But the thing is in this particular case I think it would make sense to make it that checked exception 119
in because runtime exception is an unchecked exception on by making a checked exception by throwing 120
a check the exception. 121
Are going to force the code that is going to invoke this particular method to handle it for sure. 122
Right. 123
So that's what will happen. 124
If we make it a checked exception and the benefit of that is decline here and the exceptions to we'll 125
have to catch it and let's say if in the future if there is another developer who for whatever reason 126
. 127
If you delete that Gorder then the compiler will inform him that they need to provide an exception handling 128
core. 129
And if it is a runtime exception since it would be an unchecked exception the compiler will not force 130
them to do it. 131
So it makes sense to force the hour hour hour and a person to actually handle that exception. 132
And the way they can handle it is either by e-mailing the system administrator or one of their own uppers 133
so that they can take some action they can inform the partner team that they need to fix the API. 134
So it makes sense to to make this an exception. 135
And if you want to make it a checked exception it cannot. 136
We cannot subclass an exception or we cannot subclass error so we can either subclass an exception or 137
any of its applauses or we can subclass tol'able. 138
But the item also says that it's not recommended to extend to all. 139
OK. 140
So there definitely to indict them definitely recommends that. 141
And the reason for that is it's just a little bit more confusing for the users. 142
So they suggested that if you want to extend if you want to create a checked exception you can either 143
subclause exception class which is their direct subclass of the open or one off it's subclasses one 144
of the exception classes subprocess. 145
So let's just go ahead and extend this. 146
So that's all we have here. 147
So not to go on as you know this needs to go in that cross-class So those are the those are some suggestions 148
from item 58. 149
So here it's go back into exceptions Demel we have an exception here because we need to handle it now 150
. 151
So there is already some quarter done here 152
so it needs to be hundred. 153
But a lot of times people also just how much blogs so they ignore the exception are an item fifty sixty 154
five say is that we should not do that. 155
Don't ignore the exceptions. 156
Because here around here there is you know is making a statement that you need to handle it. 157
And if you haven't time to catch block then you're defeating the purpose. 158
And the item says that if if an exception if you're ignoring an exception like ignoring gun fired not 159
him so you shouldn't be doing that because even if you ignore it sometimes you may be fined many times 160
you may be fined but sometimes the results can be disastrous. 161
Sometimes the results may not be immediately evident. 162
Maybe the error might happen you know at a later point or find some other kind of gold under time it 163
might be difficult to figure out the actual reason why the exception has been thrown because it's on 164
it's happening at a completely different location. 165
OK. 166
So you shouldn't be ignoring exceptions with them to catch blocks and even if you want to do that at 167
least you need to put some kind of a comment here stating why you are why you are not handling it why 168
it is an empty catch block. 169
OK so now let's just go ahead and let me also him this uncommented executer as you can see it says an 170
API for matching exception and gives to doctoress. 171
So those are the two items. 172
Item 65 1958. 173
Next we will look at item 63. 174
And here we have this stack grease. 175
But sometimes the issue is not reproducible so it will be difficult for the programmer to fix it. 176
So item 63 is about how we can address doc. 177
OK. 178
So that's important and I'll see you in the next lesson. 179
Item 63. 180
Thank you.