Exceptions — Java 8
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.
Unchecked Exceptions: It occurs during run time and inherits RuntimeException class.
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 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.
Finally Block is placed under the catch block and it is always executed, even if the exception is not handled. Always it will work.
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.
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…








