What is Exception Handling?5 min read

  • Post author:
  • Post category:Tutorials

Exception in Java

An exception in Java is an event that arises during the execution of a program i.e. during runtime. The occurrence of an exception in a program disrupts the normal flow of instructions. This causes the termination of the program or application and therefore needs to be handled by calling an exception handler, which deals with the exception.

An exception may occur for the invalid user input, or the files that need to be opened cannot be found., or loss of network connection.

It is a mechanism to maintain the normal flow of a program by handling runtime errors.
The main advantage of an exception handling is to ensure that even when an exception occurs, the program’s flow doesn’t break.

Let us see the type of exception in Java:

Checked: Checked Exceptions are also known as compile-time exceptions. These exceptions are checked by the compiler during compile-time. If a checked exception is found within the method then the method must be handled by try and catch or must declare an exception by throws keyword. These exceptions are not to be ignored.

Some of the checked Exceptions occur are SQLException, IOException, ClassNotFoundException, etc.

Unchecked Exception: Unchecked Exceptions are also known as run-time exceptions. These exceptions occur during the time of execution of a program that is at run-time. These exceptions are not checked during compilation.

Some of the Unchecked Exceptions occur are ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc.

Error: Errors are not an exception but are the problem that a user or programmer cannot handle. We can say that these are irrecoverable such as VirtualMachineError, OutOfMemoryError, AssertionError, etc.


Exception Hierarchy:

All exception classes are subtypes of the java.lang.Exception class. The exception types and errors are the subclasses of the Throwable class. Furthermore, they are also divided into a runtime exception and other exceptions as shown in the figure.

Exception Hierarchy
Exception Hierarchy

Handling Exception in Java:

Exception in Java is handled using five keywords:

  1. try
  2. catch
  3. throw
  4. throws
  5. finally.

Try-Catch:

This is one of the common ways to handle the exception. The combination of try and catch keyword is used in a method to catch an exception.

The try block contains the exception code and the catch block is used to handle the exception if it occurs in the try block.

The syntax for try-catch block:

Example: Java program to demonstrate try-catch:

Let us see it in an example, where the code tries to try to access the fourth element in an array which has only 3 elements:

Output:


Java Multiple Catch Block:

The code having multiple catch blocks which are followed by try block. If there are different exceptions then each block of multiple catches must contain different exception handlers. Also, one catch block is executed at a time.

The syntax for Multiple Catch Block:

Example: Java program to demonstrate the Multiple catch block:

Output:


Throw:

The throw keyword in Java is used to explicitly throw an exception from any block of code. throw keyword allows us to create a custom error or throw a custom exception.

Syntax of throw keyword:

For example, we can throw ArithmeticException when we divide the number by 3, or any other numbers, we only need to do is set the condition and throw an exception using throw keyword. Such as:

Example: Java Program to demonstrate throw:

Let us see it in a program:
Check for roll no. greater than 12, if true then allowed and if not then not allowed.

Output:


Throws

throws keyword in java is used to declare an exception. It indicates that the method may throw an exception and it’s better to handle the exception using a try-catch block to maintain the normal flow of the program.

Syntax of throws keyword:

Example: Java Program to demonstrate throws:

Output:


Finally

Finally block is used to execute the code followed by the try-catch block. finally block is always executed whether an exception is handled or not. This block appears at the end of the catch block.

Syntax of throws keyword:

Example: Java Program to demonstrate finally:

Output:

Thank you for visiting and if you didn’t understand then go through it again.
We will keep bringing dome exiting topic for you to learn.

MORE

Java – Object-Oriented Programming concept

Object-oriented programming: It is the concept of using objects in programming. It is a paradigm that uses objects and classes that aim to implement real-world …
Read More

Method Overriding and Method Overloading in Java

Here you will learn about method overloading and method overriding with syntax and various examples and also some rules for overriding. Let us start with …
Read More

Understanding Listening skill

Listening is the most important of all communication skills as we spend most of our time listening rather than other skills. It is an active …
Read More

Understanding Reading Skills

Reading skills are capacities that relate to an individual’s ability to peruse, fathom, decipher, and disentangle composed language and writings. Uncommon perusing abilities can be …
Read More

Path after BCA (Bachelor in Computer Application)

This article is specially written for BCA(Bachelor in Computer Application) and IT(Information Technology) students, keeping the mind of their computer background. This is common that …
Read More

Understanding Writing Skills

Writing skill is a vital part of communication skill. A good writing skill helps you to convey your message to one or a big crowd …
Read More