Code that is likely to fail is going to throw a checked exception.
An exception is a failure that can crash your application.
And there are two types of exceptions, checked exceptions and unchecked exceptions.
In this lesson, you're going to learn to catch checked exceptions.
An exception is a failure that the application cannot control.
And so Jeff is going to force you to catch checked exceptions.
I checked exception can occur from failing to connect to a website.
Trying to access a missing file.
Failing to access her query from a database.
So what you can take away from this is that code is more likely to fail if it interacts with outside
resources.
And so a method or constructor that's prone to failure is going to throw a checked exception, if you
want to call this method job is going to force it to handle the exception.
In other words, job is going to force you to try to run the code and catch the exception if the code
fails.
Let's go through some examples.
First things first opened up the folder, checked exceptions by following this path and the resources
and if this path is not visible to you, then please download the updated resources from GitHub.
All right, a common exception is the file not found exception, this failure is outside the control
of the application because this is a checked exception.
Job is going to force you to try and catch the exception before compiling your code.
And so inside reading Fallsburg, Java, we're going to try, no pun intended, to read the data from
Greetings Dot text each line inside the file as a greeting in nine different languages.
And so back inside the class, we need to use what's known as a file input stream.
We're going to create a new object of the file input stream class.
And we're going to use the file input stream to open a connection to our text file greetings dot text.
As it stands, this code is not going to compile because as I hover over the air and scroll down, we
can see what's wrong.
We need to handle the file, not found exception.
It's a checked exception.
In other words, there's a chance the user is going to enter a bad file name and there's nothing Java
can do except throw a checked exception.
So if you want to call this constructor, job is going to force you to handle the exception.
In other words, it's going to force you to try to run the code.
And if the code happens to fail, you need to catch the file not found exception.
And if we catch an exception, we're going to print the message that it comes with print line exception
and will get the message.
All right, now, instead of writing the code, I'm just going to visualize the runtime.
OK, I'll step into the constructor.
That's not the filing straight constructor, let me step out of it.
Step out again, OK?
I'm going to step in once more.
Hopefully now it works.
And here we can see the file input stream constructor and the constructor may throw a file not found
exception, but the file name we passed in is correct.
So it's going to find the file.
Everything is going to work, so there's nothing to catch.
But if you misspell the file.
The constructor is going to throw a file, not found exception, our code is going to catch the exception
and print the message.
Let's visualize what happens.
The Phalen bitstream constructor that were passing the file name into it's going to throw an exception
because it's not going to be able to find the file.
Greetings, Doubler.
Astarte text.
The code is going to catch the exception.
And print the error message.
All right, let's make sure we pass in the correct filename.
And after connecting to the file, you can read its data using scanner.
Now, usually, Scanner receives input from the system using System NT, but this time it's going to
receive input from the file input stream.
And what's the issue here?
Oh, I see.
OK, I'm going to use a while loop that keeps running until scanner reads every single line from the
greetings file, the while loop is going to keep running as long as the file has a next line to read.
It should run nine times.
During each run, print the value that scanner reads in the next line.
OK, one by one scandal.
Next one is going to read every single line inside greetings text and after it reads all nine lines,
the while loop is going to break.
So let's run the code and see what happens.
And it prints every line from the greetings file Perfect's.
I'm going to place breakpoints to visualize the runtime.
Input stream, connect to the file and now scanner is going to receive all of its input from the file
input stream in line by line scan, the next line is going to read all the input from the readings file.
And one scanner reads every line once there isn't a next line anymore, the wire loop is going to break
and it ends.
All right, I'm going to go back and misspell the file.
This line of code fails and throws an exception, our code catches the exception and prints an error
message.
OK, before we wrap up with this class, don't forget to close the scanner object.
All right, now open reading files to Java.
This code is the same as before, but this time the low data function contains code that throws an exception,
the same code as before.
Here we can do two things we can use to try catch block inside the function.
Or we can declare that the code inside this function could fail and throw a file not found exception.
Woops, I'll have to autocomplete it get.
And now, because low data throws an exception, Java forces, whoever is calling it, to try to call
this function.
And if the function that we're calling happens to throw an exception, if it happens to fail, you need
to catch the file not found exception.
And after catching the exception, print the message that it comes.
All right, this should function the same way I run it.
And beautiful.
Here's another example of a checked exception, the malformed you are the exception.
It's a failure that once again the application can't control.
So job is going to force you to try and catch the exception before compiling your code.
So inside Pursey World Java, we're going to try to pass the Google dot com your.
To create a new object of the moral class.
HTTPS colon double slash.
Without Google dotcom.
As it stands, this code is not going to compile once again, hover over there to see what's wrong.
The euro constructor throws a malformed Eurail exception, so if you want to call this instructor,
JAV is going to force you to handle the exception.
In other words, it's going to force you to try to run the code.
And if the code happens to fail, you need to catch the malformed, you are an exception.
And after catching the exception, print the message that it comes with.
All right, if you're on this code.
It should work because DRL is well formed.
And indeed, it does.
Now, as a side note, the euro is composed of a protocol, a host and a path.
So from the Yooralla object, we can call some methods defined in the early class, so I'll print the
protocol.
I'll print the hosts.
You know, get the path.
All right, now I'm going to add a path of images so that we can get an interesting output.
Run the code.
In passing, the URL was successful good.
Prince, the protocol, the host and the path.
Now, if you input a malformed, you are a.
This line of code is going to throw an exception, nothing after it runs instead of a code catches the
malformed.
You are an exception and it prints its message.
Unknown protocol, a bunch of nonsense.
OK.
Now, open pass, you are allowed to dodge other.
Once again, the code is the same as before, but this time the parts you are afunction contains code
that throws an exception.
Here we can do two things we can use to try catch block inside the function, or we can declare that
the code inside this function throws a malformed.
You are an exception.
And because past you are out throws a checked exception job is going to force whoever is calling it
to try to run the code.
And if the code happens to fail, you need to catch the malformed, you are an exception.
And after catching the exception, print the message that it comes with.
I run the code.
This works.
I'll try again where they bade you all.
And the parts you are afunction that we tried to call throws a malformed you are an exception, that
exception gets cut and it's because the protocol doesn't exist.
In this lesson, you learn to catch checked exceptions.
An exception is a failure that can crash your application.
There are two types of exceptions.
There are checked exceptions and unchecked exceptions.
I checked exception is a failure that the application cannot control.
A method or constructor that is prone to failure is going to throw a checked exception.
And if you want to call this method, job is going to force you to try to run the code and catch the
exception if the code fails.