NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

In this lecture, you will learn how to use the throws keyword in exception handling. For this purpose

we'll use the CheckedException class. Double click to maximize. We'll move this part of code which you've already

done into an other method.

So copy it, within this class define a method

void fileRead or void readFile.

It's not really reading a file. Paste it, it's just opening a file.

Right now we are not even passing the path of that file and delete this code from here.

Create an instance of this class, CheckedException ce = new CheckedException();

ce.readFile();

Now for some reason if this readFile does not want to handle this CheckedException, it wants whichever method is

calling the readFile, it wants that method to handle this FileNotFoundException.

We can use the throws keyword, throws FileNotFoundException.

Then we can delete this try catch entirely and the compiler won't complain. Without declaring it as

throws,

if I try to remove this and also remove the try catch, immediately the compiler complains about it.

You have already seen it earlier.

It says unhandled exception type FileNotFoundException. So let's declare it using

throws FileNotFoundException.

And the compiler is happy.

But now, the main method needs to handle the exception.

The compiler immediately says Unhandled exception type FileNotFoundException because using the

throws class, we have declared that whichever method is calling the readFile needs to handle this

FileNotFoundException. So we have to surround this with try and then catch(FileNotFoundException e).

Open the flower bracket, sysout, File Not Found.

Close the try block here.

The compiler is happy. But even now if I run this program, right click, Run as -> Java application, we get

a File Not Found because we are not passing any valid file input here.

And an exception is thrown

which is FileNotFoundException.

We are handling that inside the main method.

Now if I remove, if I don't want to handle it even in the main method, I can remove this try and catch.

The compiler ofcourse will complain immediately.

We can use the throws here as well,

throws FileNotFoundException.

Eventually the default exception handler of the JVM will be kicked in

and that will handle the error message. But the end user will see a unfriendly message

as you know. So let's see,

Run As -> Java application. And the default exception handler of the JVM is handling it.

That is the reason, you as a end user see the entire exception message.

So the throws keyword can be used to declare that a method is not going to handle the checked exception but

the caller needs to handle it.

And if at the end nobody handles it, everybody declares it using throws, it will go to the JVM's default

exception handler.
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.