Pavan reddy
4 min readMay 16, 2021

--

An unexpected event that disturbs the normal flow of the program is called an exception. In similar words, if you were watching a video on youtube suddenly got interrupted in watching video due to internet disconnectivity.

Real-time example for Exception

What is the meaning of exception handling?

Exception handling does mean that resolving an exception. We have to define an alternative way to continue the rest of the process in normal flow. This way of defining alternatives is nothing but exception handling. In another word, we can say it is graceful termination.

example for exception handling

Stack frame:

JVM will generate a separate stack for every thread at the time of creation. All
method calls performed by that thread will be stored in that stack. Each entry in the stack is called a “stack frame”.
In the stack, every method call is completed then JVM removes the corresponding entry from the stack. All method calls completed then JVM destroys the empty stack and terminates the program normally.

Default Exception Handling in Java:

It’s nothing but whenever an exception is raised and you are not handling it then that process will be terminated abnormally this termination is taken care of by JVM. In similar words, if you are writing a program that is used to fetch data from the file and do some operation but if the file is not present and you were not handling it then we cannot continue the rest of the process. There default exception handling comes into the picture.

While calling the readingFile() method then Exception will be raised like fileNotFoundExecption and we are not handling it, then that method is responsible to create Exception object which contains name,description and location of exception.

That object will be handover to calling function,if calling function not handling it, then it is also do the same process and handover it to main method and main is not handling. Then main also creates exception object and handover JVM, then the program will be terminated in abnormally by JVM.

Exception Hierarchy:

Throwable:
Acts as a root for exception hierarchy and child of Object class. Throwable class contains the following two child classes.

Exception:
These exceptions are caused by our program in most of the cases and these are recoverable.Ex: IllegalArgumentException This is Thrown to indicate that a method has been passed an illegal or inappropriate argument.

Error:
These errors are not caused by our program in most of the cases and these are due to lack of system resources and these are non-recoverable.EX: AbstractMethodError This is Thrown when an application tries to call an abstract method.

Types of Exceptions:

Checked Exception:
The exceptions which are checked by the compiler whether programmer
handling or not, for smooth execution of the program at runtime, are called
checked exceptions.

Unchecked Exception:
The exceptions which are not checked by the compiler whether programmer
handing or not ,are called unchecked exceptions.

Note:
RuntimeException and its child classes, Error and its child classes are unchecked and all the remaining are considered as checked exceptions.

Five keywords used to handle Exception:

  1. Try: This block is used to write risky(Exception) code. Try block must be followed by catch or finally block. We cant use Try alone.
  2. Catch: This block is used to maintain exception handling code. We cannot use catch block without try block .
  3. Finally: This block meant for cleanup activities related to try block. It is executed whether an exception is handled or not.
  4. Throw: Sometimes we can create Exception object explicitly and we can hand over to the JVM manually by using throw keyword.
  5. Throws: We can use throws keyword to delegate the responsibility of exception handling to the caller method. Then caller method is responsible to handle that exception.
Example code for try/catch and throws

Note:
1. whenever checked exception rised recommended to use try/catch block instead of throws .
2. Within the try block if anywhere an exception raised then rest of the try block won’t be executed even though we handled that exception. Hence we have to place only risk code inside try block and length of the try block should be as less as possible.

--

--