Exceptions - Checked

17 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.77
List Price:  €131.29
You save:  €37.51
£80.84
List Price:  £113.18
You save:  £32.34
CA$137.53
List Price:  CA$192.54
You save:  CA$55.01
A$155.84
List Price:  A$218.18
You save:  A$62.34
S$136.11
List Price:  S$190.56
You save:  S$54.45
HK$783.07
List Price:  HK$1,096.33
You save:  HK$313.26
CHF 91.04
List Price:  CHF 127.46
You save:  CHF 36.42
NOK kr1,101.67
List Price:  NOK kr1,542.39
You save:  NOK kr440.71
DKK kr700.10
List Price:  DKK kr980.18
You save:  DKK kr280.07
NZ$169.82
List Price:  NZ$237.75
You save:  NZ$67.93
د.إ367.23
List Price:  د.إ514.14
You save:  د.إ146.90
৳10,949.65
List Price:  ৳15,329.95
You save:  ৳4,380.30
₹8,336.20
List Price:  ₹11,671.02
You save:  ₹3,334.81
RM478.35
List Price:  RM669.71
You save:  RM191.36
₦129,687.03
List Price:  ₦181,567.03
You save:  ₦51,880
₨27,766.58
List Price:  ₨38,874.33
You save:  ₨11,107.74
฿3,679.68
List Price:  ฿5,151.71
You save:  ฿1,472.02
₺3,248.23
List Price:  ₺4,547.65
You save:  ₺1,299.42
B$520.25
List Price:  B$728.38
You save:  B$208.12
R1,929.28
List Price:  R2,701.08
You save:  R771.79
Лв183.27
List Price:  Лв256.58
You save:  Лв73.31
₩137,435.75
List Price:  ₩192,415.55
You save:  ₩54,979.80
₪376.07
List Price:  ₪526.51
You save:  ₪150.44
₱5,756.72
List Price:  ₱8,059.64
You save:  ₱2,302.91
¥15,460.95
List Price:  ¥21,645.95
You save:  ¥6,185
MX$1,709.87
List Price:  MX$2,393.89
You save:  MX$684.02
QR364.04
List Price:  QR509.67
You save:  QR145.63
P1,385.73
List Price:  P1,940.08
You save:  P554.35
KSh13,119.90
List Price:  KSh18,368.39
You save:  KSh5,248.48
E£4,832.51
List Price:  E£6,765.71
You save:  E£1,933.20
ብር5,668.96
List Price:  ብር7,936.77
You save:  ብር2,267.81
Kz83,691.63
List Price:  Kz117,171.63
You save:  Kz33,480
CLP$95,228.57
List Price:  CLP$133,323.80
You save:  CLP$38,095.23
CN¥723.85
List Price:  CN¥1,013.42
You save:  CN¥289.57
RD$5,936.90
List Price:  RD$8,311.90
You save:  RD$2,375
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$227.72
List Price:  FJ$318.82
You save:  FJ$91.09
Q775.98
List Price:  Q1,086.41
You save:  Q310.42
GY$20,874.15
List Price:  GY$29,224.64
You save:  GY$8,350.49
ISK kr14,123.58
List Price:  ISK kr19,773.58
You save:  ISK kr5,650
DH1,010.74
List Price:  DH1,415.07
You save:  DH404.33
L1,790.82
List Price:  L2,507.22
You save:  L716.40
ден5,773.75
List Price:  ден8,083.48
You save:  ден2,309.73
MOP$804.95
List Price:  MOP$1,126.96
You save:  MOP$322.01
N$1,917.07
List Price:  N$2,683.98
You save:  N$766.90
C$3,695.63
List Price:  C$5,174.03
You save:  C$1,478.40
रु13,325.78
List Price:  रु18,656.63
You save:  रु5,330.84
S/372.26
List Price:  S/521.17
You save:  S/148.91
K380
List Price:  K532.02
You save:  K152.01
SAR375.10
List Price:  SAR525.15
You save:  SAR150.05
ZK2,561.66
List Price:  ZK3,586.43
You save:  ZK1,024.76
L466.89
List Price:  L653.66
You save:  L186.77
Kč2,369.71
List Price:  Kč3,317.69
You save:  Kč947.98
Ft36,989.30
List Price:  Ft51,786.50
You save:  Ft14,797.20
SEK kr1,091.25
List Price:  SEK kr1,527.80
You save:  SEK kr436.54
ARS$87,116.28
List Price:  ARS$121,966.28
You save:  ARS$34,850
Bs690.90
List Price:  Bs967.28
You save:  Bs276.38
COP$391,948.05
List Price:  COP$548,742.95
You save:  COP$156,794.90
₡50,105.09
List Price:  ₡70,149.13
You save:  ₡20,044.04
L2,463.18
List Price:  L3,448.55
You save:  L985.37
₲738,132.47
List Price:  ₲1,033,414.98
You save:  ₲295,282.51
$U3,839.04
List Price:  $U5,374.81
You save:  $U1,535.77
zł404.45
List Price:  zł566.25
You save:  zł161.79
Already have an account? Log In

Transcript

So welcome back guys to the next tutorial. Today we're gonna discuss really important topic. So it's about exceptions and exceptions handling. So we really need to understand what this is a lot of times in our coding we do mistakes and there are check them. There are like multiple sort of exceptions. We really need to handle our exceptions well, so that user experience of proper thing and he doesn't have a bad experience using our application.

There are really two kinds of exceptions. First is checked exceptions and second is runtime exceptions. If I tell you what exceptions are, it is basically just a problem that happens in our program. It can either happen during coding like before compiling so it can find out okay, if it's an exception, or it can actually happen during runtime also something which happens during Before completion, and it's the compiler can check, then, that is basically checked exceptions because our ID and compiler is checking for that. And we can certainly fix it before running our program. And there are runtime exceptions, which are just caused by our bad programming skills.

So sometimes let's say we're trying to divide something by zero, which is gonna, which is going to show me some exception, and our compiler is not gonna catch that it's gonna catch it only at the time of combining no not at the time of runtime not before compiling, not when we are typing in. So something like I have an item in my area and my area consists of only 10 items, and I'm trying to access the 11th item. At that point, it's going to give me an exception because my array does not even contain the 11th item, right. So that is a runtime exception. So guys, let's first discuss checked exceptions with the help of chord and maybe let's create a couple of classes, neural class. Maybe exception demo in class, and let's create one more class.

So what I'm gonna do is maybe, maybe account. So what I'm trying to do here is I'm just gonna create a small dummy application to withdraw some money from my account. So let's, let's take some, let me just create a method, maybe public wide withdraw and give some amount to withdraw. That's it. So before we're drying Let's say my application, my application is running a MySQL database. It is basically going to access my database, pull out my account information, my balance my column or everything and then do some calculations and then basically allow gonna allow me to withdraw some money or not.

So let's create a small method, maybe like some which can handle some exception, right? So maybe public is canceled, which can handle some exception, which can demo some exception thing and which throws some exception. So public. Let's make it as a connection type, because I'm going to create a connection to the database. So this program is kind of multi teaching. So I'm going to teach you how to connect to a database Also, I'm going to teach you what exception handling is.

So let's say public connection. We just name it as get connection. This and maybe there's two left connection, something like that. And create a connection, reference, maybe none at this point. Okay, it's gonna give me some things okay what I want because the type cannot be resolved. So I'm just going to import connection from my Java SQL in both classes, and pretty much gonna import everything so it doesn't bother me later.

When I do connection equals to the way I create a database connection is driver, manager dot get connection. There are different overloads I am going to use the third one, which says it's gonna require a URL, basically the address to my database, user password, which is the credentials to my database. And this method throws a sequel exception if you guys notice So what does that mean? If at all Java faces any problem accessing my database or finding my database, it's gonna throw an exception that, Okay, guys, I have a problem. So basically, it's just throws exception. It requires these two things.

So first, let's declare these two things. String URL. So URL is my database. What my URL is gonna be guys, it's gonna be because I'm saying it's a jade, MySQL JDBC, which is the driver for connecting to the databases my sequel address to my database, and here you can provide the hostname or IP address of the server where your database is deciding. I'm just giving you localhost I now, I don't even have a database running on my local host either, but I'm just giving him now. And maybe my Port 3306 for MySQL.

This is the URL for my database. String user. Let's see, I have a very smart user called ABC. And train. Pass word. Very, very strong fast.

Whereas code, I'm just taking something. So I'm passing all this right. Now what it's conveying policy again, it's complaining. complains too much, right? It says unhandled exception type SQL exception. All right.

We saw that right. This method throws a sequel exception connection, how we can fix it. When I do this, it gives me two options. Either add throws declaration or asset Round this way with my try catch block. So we'll add throws declaration, that means this get connection method is just gonna declare that I'm going to throw an exception if you don't handle this. That's it.

So it's job is done. What it does is it just added to SQL exception, key word in front of the method. So whenever I'm gonna call this method, it's just gonna say, I'm gonna throw exception if it's not handled properly, wherever the method is called. So now it becomes the responsibility where I'm calling a method to handle that exception. We are done from this side. So let's say I am just providing an API to my other team.

So my job is done that way. Now it's his job to completely handle that exception. So let's say I'm calling this connection whether in my withdraw method, because first I'm going to create a connection to my account, and then only I'm gonna draw some, some dollar side. So again, it says Okay, I have a problem. And it seems again, unhandled exception type already. So let me again, throw something and I just want to throw the exception.

And again acid, so declaration that I'm going to throw us equal type exception just does that and then later I can maybe withdraw some amount or do something. And the next slide basically, this is that so let me call these in my other class account, maybe ACC equals to new account, ACC dot withdraw some money. I want to draw a small amount like hundred dollars. So now it says unhandled exception type. See, it's propagating all those things right. Here I just said Oh, going up to this eclipse section where I'm calling this asset.

Okay, I'm going through these. I'm just gotta warn my method which is going to use me that okay, I threw a sequel exception if it's not handled properly. It just declared that now in my some of the classes main method, I'm calling this account on Java. It's again reflecting. Okay, I need to be handled because those methods to a sequel exception. So if I just add a throws declaration, it hits off it.

Now let's see what happens when I run it. So when I run it oh my gosh, I handled it still it's showing me all these exception slides these red color things with bothers me. What it does is it says no suitable driver found for JDBC MySQL, so there's no driver. It's gonna throw an exception no Sorrell found nothing. Where are the exceptions coming from? exceptions demo dot java, line number nine.

This represents the line number. All right, guys. So this is the same Class line number nine is this. Now from where this is coming, it's coming from account or Java nine number 18. All right. And from where it's getting, it's getting from account dot java from the get connection method.

Line number 12. It's getting from there, say it has propagated to the metal from it where it's coming. So I definitely don't want to show these things to my user, I want to show something decent. What I can do in my method here is instead of adding a true SQL exception, I can just add a try catch block. When I do this, what it's gonna do is, it's gonna try the code in my try block, it's gonna run it. If it faces any exception, then it's gonna go into the catch block, which has the sequel exception, type and kind of a declaration of E which is gonna call all the methods from my SQL exception class.

So SQL exception is again a class. And all the parents class from for these exceptions is like, exception class for all the checked exceptions, the paid in classes, exceptions class, everything is derived from that. So, the is there, there are some methods like e dot print stack trace, which is still gonna do the same thing. So let me run this code. And it printed all the status that you see before. So, if you don't want to show these things to us, or what we can do is maybe just handle these exceptions on a battery, like e dot maybe.

Honestly, I want to get some methods like we don't get message. So I can show to the user, okay, you fail because there's no suitable time. I just want to show this thing. I do not want to show which line of code it's failing and You don't need to panic that much. I can maybe give them some optional thing. Maybe the server is down right now try again and try again.

At some other point. Maybe I can just tell them okay, right now the server is not accessible. I don't maybe even want to show what what the show is. I can say, yeah, just this try again later server is not accessible. So when I run this, you'll only see Okay, try again at some other point. That's it.

So this is the way to handle exceptions. And if this thing is not happening, I want to handle the code nicely and do some other operations. Two things we can do for checked exceptions, source declaration, or either a try catch block. Now with the try catch block. The catch block cannot exist without a time block. This is quite often But just to show you guys if I command this out, it shows me again on a show that there is an indexer on the token cache because there is no child.

So I really need this for caching. But for try, that's not the case. We have a finally keyword which is quite handy in some cleaner situations, which always doesn't matter if the try block is executed or the catch block is executed. But finally, always, so let me show this to all these things. By run this, see try again at some other point went to the catch block because try is not successful and always executes because it's and finally to see if I run this. Let me show you how tri works.

First line So this is my first line before trying the withdraw method. This is my second. So maybe after. After trying. If I do this, notice guys, I'm running this, it says first line, it went into try block executed this. When it came to the account or withdraw, it saw the exception.

It didn't execute any code after this. This line is not printed right after trying the withdraw method, nothing is better. It directly went to the catch block and executed the commands over there. And then it went to the finally block. Let's say I'm not calling this I'm not calling this and I'm just calling the exception class because that is apparently Last exception, and I am now running this code. What happens is first line after trying the withdraw method, this line is also executed, because there's no withdraw.

And it doesn't execute this try again at some other point. Because the trial was successful it didn't go to the cache block. It now again came to the final block always experience. So either trial is going to be executed or caches gonna be executed, but finally will always execute. And if we have a Finally, we do not really need a cache block until we really want to handle something my code will done properly right now. See exactly the same.

Try block is needed for cash but cash is not mandatory for a try. But try will also not run alone. Try needs either cash or a finally simple to understand is cash needs. Try client it's either a cat or a Finally, usually put on everything. This is basically our try catch blocks. The way we can handle exceptions, show proper messages to the users and make our program a little better improve some of the coding skills.

This is pretty much it guys for this tutorial, because I'm thinking to make a different tutorial to cover the runtime exceptions because otherwise it's gonna be too much to absorb. Right now it's okay you guys can just practice something and to some cooling, you can find this code on Udemy for friends and try some of your codes. I can give you some examples like you can try to access a file, read a file using the file reader class of Java and give a file path which does not exist and you should be able to see exception there and then try to handle it that way. One example to see 10 exceptions. Maybe try that code and try to handle some exceptions. It's very important.

You should be good, easy to absorb. And in next class, we'll just cover the runtime exceptions. Thank you guys. I would just say practice more, and thanks for attending.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.