Notes
![]() ![]() Notes - notes.io |
implicitly. That is when something goes wrong in the code,
the exceptions were being created by the JVM and we were catching them. Similarly in case of checked
exceptions there were methods that were used which were throwing the checked exception and we needed
to handle them or declare them using throws class. In this lecture,
you'll learn how to throw exceptions explicitly. For that purpose you will be using the throws keyword.
Right click on the default package, New -> Class, ThrowKeyword. Let's name our class ThrowKeyword, select the main
method.
Finish.
Control+d to delete it.
To throw our own exception we use the throw keyword and then we need to instantiate an exception. We use
the new keyword and then whatever exception
it is. Typically we can throw any exception, ArrayIndexOutOfBound, etc; But it doesn't make any sense
because if the index is really out of bound the JVM itself will throw that exception.
Typically we throw a RuntimeException. If something goes wrong in our application logic at runtime
or if there is something that is not expected according to our logic, then we can throw this RuntimeException.
The constructor of RuntimeException or any exception for that matter usually takes a string
as input as well.
There is a default constructor and we can pass in, the second constructor also takes a string which
could be the exception message.
Let's say Funds not available.
That could be our business reason why we are throwing this exception. Right click, Run As -> Java application
and our exception is thrown.
Similarly we can also throw checked exceptions. Checked exceptions are those which inherit from exception
like FileNotFoundException, etc. But FileNotFound will be thrown by JVM automatically if you're trying to open
a file. So we'll throw new Exception. Checked exception
needs to be handled.
So it's says unhandled exception type exception.
So either we surround this with a try catch.
Or we declare, close the try. Or we declare it as throws.
So using the throw keyword you can create an instance of any exception, either a RuntimeException or a unchecked
or a checked exception from our application. Depending on where we want to use it for a particular business
reason we can always throw the inbuilt exceptions. In the next few lectures, when you create your own
custom exception classes,
once again you could use the throw keyword to throw those exceptions as well.
![]() |
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