In this article Exception Handling in Java we  give the information about in Java, Exception Handling is a very powerful technique by which runtime errors are handled. Due to which the normal flow of the program is maintained.

Exception Handling in Java:

Exception in Java

In Java, an exception is an event that stops the normal flow of the program. Whenever an exception occurs in the program, the execution of the program ends and we get an error message generated by the system. The good thing is that we can handle these exceptions.

In other words, “An exception is an unwanted or unexcepted event, which occur during the execution of a program that is run time error, that disrupts the normal flow of the program.”

For Example:

class Exception_test

{

public static void main(String [] args)

{

System.out.println(1);

System.out.println(2);

System.out.println(3);

System.out.println(4);

System.out.println(100/0);

System.out.println(5);

System.out.println(6);

}

}

Output:

Error.

Exception Handling in Java:

In Java, Exception Handling is a very powerful technique by which runtime errors are handled. Due to which the normal flow of the program is maintained.

In other words, “Exception handling is an important feature of Java through which we can handle runtime errors caused by exceptions.”

Let us understand this through an example. Suppose you ordered some item online. But due to some reason the delivery could not be done. A good company can handle this problem and reship the item. So that we get the goods on time.

Similarly, errors can occur while executing code in Java. A good exception handling can handle these errors and get the program running again. So that the user gets a good experience.

Difference between Exception and Error:

  • Object is the parent class of all the classes in Java
  • Throwable is the parent class of the exception class. 
Sr. No. Exception

Error

1 In most of the cases, Exception is occurred by our program. Error is occurred because of lack of system resources (memory, RAM, Processor etc.) not by our programs and thus programmer cannot do any things.
2 Exception are recoverable that is programmers can handle them using try catchy block. Error are not recoverable that is programmers cannot handle them to their level.
3 There are two types:

1.        Compile time exception / Checked exception

2.        Runtime exception / Unchecked exception

There are only type:

1.        Runtime exception / Unchecked exception

Types of Exception:

There are two types of exception in Java such as flow:-

  1. Checked Exception / Compile Time Exception
  2. Unchecked Exception / Run Time Exception

Checked Exception –

Checked exception is also called IOException, SQL exception. These are checked by the compiler at compile-time and the compiler prompts the programmer to handle these exceptions.

Following are some examples of Checked exceptions:-

  • Trying to open a file that does not exist – FileNotFoundException
  • Trying to read back from the end of the file.

Example –

If you use the FileReader class to read data from a file in your program and if the file in the constructor specified does not exist, then FileNotFoundException will occur and the compiler will tell the programmer to handle the exception. will ask for

import java.io.File;

import java.io.FileReader;

public class FilenotFound_Demo

{

public static void main(String args[])

{

File file = new File(“F://file.txt”);

FileReader fr = new FileReader(file);

}

}

Output:-

C:\>javac FilenotFound_Demo.java

FilenotFound_Demo.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown

FileReader fr = new FileReader(file);

Unchecked Exception –

This exception is also called Runtime Exception. These happen due to mistakes in programming.

These exceptions are not checked in compile-time but are checked in run-time. Below are some common unchecked exceptions:-

  • Not using API properly – IllegalArgumentException
  • Not initializing the variable – NullPointerException
  • Accessing array out of bounds -ArrayIndexOutOfBoundsException
  • Dividing a number by 0 – ArithmeticException

You can think of it like this – “If it’s a runtime exception, it’s your fault.”

Example – If you have declared the size of an array as 5 in your program and you try to call the 6th element of the array then ArrayIndexOutOfBoundsException will occur.

public class Unchecked_Demo

{

public static void main(String args[])

{

int num[] = {1, 2, 3, 4, 5};

System.out.println(num[5]);

}

}

Output:-

The following exception will appear in its output.

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5

at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)

Difference between Compile time (Checked) Exception and Runtime (Unchecked) exception:

Sr. No. Checked Exception

Unchecked Exception

1 The exception which are checked by the compiler for smooth execution of program at run time The exception which are not checked by the compiler and directly taken care by JVM
2 Checked exception is commonly occurred exception. So the compiler  takes very much care about this exception Unchecked exception is rarely occurred exception. So the compiler does not takes very much care about this exception
3 // Example:

import java.io.FileInputStream;

class abc

{

public static void main(String [] args)

{

FileInputStream obj=new FileInputStream(“D:\tet.txt”);

}

}

Output:

abc.java:6: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown FileInputStream obj=new FileInputStream(“D:\tet.txt”);

^

1 error

// Example:

import java.io.FileInputStream;

class te

{

public static void main(String [] args)

{

try

{

FileInputStream obj=new FileInputStream(“D:\tet.txt”);

}

catch (Exception e)

{

System.out.println(e);

}

}

}

Output:

java.io.FileNotFoundException: D:       et.txt (The filename, directory name, or volume label syntax is incorrect)

4 Examples of Checked exceptions:

  • File Not Found Exception
  • No Such Field Exception
Examples of Unchecked Exceptions:

  • Arithmetic Exception
  • Null Pointer Exception
  • Array Index Out of Bounds Exception
  • Security Exception

Advantages of exception handling in java:

  1. Exception handling ensures that the flow of the program is maintained when an exception occurs.
  2. Through this we can identify the types of errors.
  3. Through this we can write error-handling code separately from normal code.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *