Sitemap

Exceptions — Java 8

3 min readDec 2, 2020

--

Press enter or click to view image in full size

Exceptions Hierarchy

Exception and Error are the children of the Throwable class. If Java Virtual Machine can not recover anything it throws an error. An error can not be handled. Exceptions can be handled. Let's start.

The exception is the parent class of all entire Exceptions. There are two types of exceptions; Checked (unwanted) and Unchecked(unexpected).

Checked Exceptions: We can not be able to run the code. It occurs during compile time and does not inherit the RuntimeException class.

While you are writing the code, the editor will show us the exception. We have to handle it immediately.
Press enter or click to view image in full size

Unchecked Exceptions: It occurs during run time and inherits RuntimeException class.

Press enter or click to view image in full size
When we run the code, we’ll take the exceptions which I mentioned above.

Throw Keyword (without an “s”)

If we want to create an exception, we use the “throw” keyword. The throw does not handle the exception, it is used for manually throwing an exception.

We can create a checked or an unchecked exception with “throw”.

We have to know that while we are using the throw, it will block the code after “throw”. The code in the same block under the throw keyword will not run, will be unreachable.

Try&Catch Blocks and Finally Block

Try-Catch Blocks are used to handle any exceptions, checked or unchecked it doesn't matter. There are two parts:

Get Koxal’s stories in your inbox

Join Medium for free to get updates from this writer.

Try Block contains the exception.

Catch Block contains the class or parent class of the exception that it will occur.

There is no -1. element in array but, we are using try-catch blocks and handling the exception. We are keeping the exception message at “e” and we are getting to write it. So we are reading “ -1 “.
When we use the try-catch block, the code under the exception continues to run. We can also get the message “It is done”.

Finally Block is placed under the catch block and it is always executed, even if the exception is not handled. Always it will work.

We tried to catch a NoSuchElementException but, it was an ArrayIndexOutOfBoundsException. So we couldn't handle it.
Even if we can not handle the exception, finally block will get executed but, the code after the try-catch block will not execute. Because the exception couldn’t be handled.

Throws Keyword

“Throws” is used within the method signature to handle the checked exceptions.

“Throws” keyword throws the exception for just this method, therefore if we invoke this method from anywhere, this method will have the exception.

There is a checked exception.
We use the “throws” keyword and handle the exception.
When we invoke the method, the method comes with its exception. So we need the “throws” keyword again.

How can we handle the exceptions?

We use a try-catch block to handle any exceptions. Also, we use sometimes the “throws” keyword but, it has a disadvantage while calling or inheriting the method. We always want our code to be smooth so, we handle checked and unchecked exceptions.

Examples of the exceptions:

NoSuchElementException, NoSuchWindowException, NoSuchFrameException, NoAlertPresentException, StaleElementException, TimeoutException, NoSuchSessionException…

--

--