Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction:
2.
Types of Exceptions and Error:-
3.
Hierarchy:
4.
Catching Exceptions:
5.
FAQs:
6.
Key Takeaways: 
Last Updated: Mar 27, 2024

Introduction to Exception Handling

Author Nishant Rana
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction:

Exceptions are the events that prevent the normal flow of a program. It interrupts the program by terminating it and gives the computer-generated error message. These exceptions can be handled easily in java.

Exceptions can occur because of many reasons like dividing a number by 0 or accessing an out-of-bound index of an array.

Also read, Duck Number in Java and Hashcode Method in Java

Types of Exceptions and Error:-

  1. Checked Exceptions: These are the exceptions that are notified to the user by the compiler at the time of the runtime. For example, if you are accessing a file using the FileReader class and the file passed to the object does not exist then it will give a FileNotFoundException exception.
     
  2. Unchecked Exceptions: These are the exceptions that occur only when the program runs. This is the reason why they are also called Runtime exceptions. They are ignored at the compile time. For example, if we are accessing the out-of-bound index of an array then we would get an ArrayIndexOutOfBoundsException.
     
  3. Errors: These are the unexpected operation performed by a programmer that results in the abnormal working of the program. These errors can be identified once the code is compiled or run. Some of these error prevents the code from compiling and running. For example, if we are dividing a number by zero then we would get a DivisionByZero error. Also, if the stack overflow occurs then the error will arise.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Hierarchy:

All the exceptions and error classes come under the java.lang.Exception class. Both the Exception and Error classes are the subclasses derived from the Throwable class.

 


Also see,  Swap Function in Java

Catching Exceptions:

Exceptions can be handled by catching them inside the try/catch block. The code which is expected to generate the exception is surrounded by a try/catch block. In the try block, we add the code, and then we need to add the appropriate catch block to catch the exception caused by the code in the try block.

Following is the general syntax for the try/catch block:-

try {
  // Protected code
catch (ExceptionName e) {
  // Catch block
}

 

Let us understand it with an example:-

public class Main{
public static void main (String[] args){
        try{
            int a[] = new int[10];
            System.out.println(a[11]);
        } 
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception thrown  :" + e);
        }
}
}

 

In the above code, we made an array of size 10. But we are trying to print the 11th index value of the array which does not exist. Hence, it will generate an exception that will be caught by the catch block as the same exception(ArrayIndexOutOfBoundsException) has arisen.

There can be multiple catch blocks to handle different exceptions caused by a code. The syntax for multiple catch blocks is:

try {
  // Protected code
catch (ExceptionName1 e1) {
  // Catch block
catch (ExceptionName2 e2) {
  // Catch block
catch (ExceptionName3 e3) {
  // Catch block
}

 

There is also one FInally Block which we can add at the end of all the try/catch blocks. The code enclosed in this block is always executed no matter if the exception arises or not.

Following is the syntax for finally block:

try {
  // Protected code
catch (ExceptionName1 e1) {
  // Catch block
catch (ExceptionName2 e2) {
  // Catch block
catch (ExceptionName3 e3) {
  // Catch block
}finally {
  // The finally block always executes.
}

 

FAQs:

  1. What are Exceptions?
    • Exceptions are the events that prevent the normal flow of a program. It interrupts the program by terminating it and gives the computer-generated error message. These exceptions can be handled easily in java.
  2. What is a Finally Block?
    • It is a block of code added at the end of the try/catch blocks. The code enclosed in this block is always executed no matter if the exception arises or not.

Key Takeaways: 

In this blog, we have covered the following things:

  1. We first discussed what are Exception and their types.
  2. Then we discussed how to handle the exceptions.
     

If you want to learn more about Programming Language and want to practice some questions which require you to take your basic knowledge on Programming Languages a notch higher, then you can visit our here

Until then, all the best for your future endeavors, and Keep Coding.








 

Next article
Difference between Error and Exception
Live masterclass