Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Exception Handling in Java

Java, Exception Handling

Uploaded by

Sahil Mali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Exception Handling in Java

Java, Exception Handling

Uploaded by

Sahil Mali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Exception Handling in Java

The Exception Handling in Java is one of the powerful mechanism to handle the
runtime errors so that normal flow of the application can be maintained.

In this page, we will learn about Java exceptions, its type and the difference between
checked and unchecked exceptions.

What is Exception in Java


Dictionary Meaning: Exception is an abnormal condition.

In Java, an exception is an event that disrupts the normal flow of the program. It is an
object which is thrown at runtime.

What is Exception Handling


Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.

Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the
application. An exception normally disrupts the normal flow of the application that is
why we use exception handling.

Let's take a scenario:

1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
8. statement 8;
9. statement 9;
10. statement 10;

Suppose there are 10 statements in your program and there occurs an exception at
statement 5, the rest of the code will not be executed i.e. statement 6 to 10 will not be
executed. If we perform exception handling, the rest of the statement will be executed.
That is why we use exception handling in Java.

Hierarchy of Java Exception classes


The java.lang.Throwable class is the root class of Java Exception hierarchy which is
inherited by two subclasses: Exception and Error. A hierarchy of Java Exception classes
are given below:
Types of Java Exceptions
There are mainly two types of exceptions: checked and unchecked. Here, an error is
considered as the unchecked exception. According to Oracle, there are three types of
exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error

Difference between Checked and Unchecked


Exceptions
1) Checked Exception

The classes which directly inherit Throwable class except RuntimeException and Error
are known as checked exceptions e.g. IOException, SQLException etc. Checked
exceptions are checked at compile-time.

2) Unchecked Exception

The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

3) Error

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.


Java Exception Keywords
There are 5 keywords which are used in handling exceptions in Java.

Keyword Description

try The "try" keyword is used to specify a block where we should place
exception code. The try block must be followed by either catch or finally. It
means, we can't use try block alone.

catch The "catch" block is used to handle the exception. It must be preceded by
try block which means we can't use catch block alone. It can be followed by
finally block later.

finally The "finally" block is used to execute the important code of the program. It
is executed whether an exception is handled or not.

throw The "throw" keyword is used to throw an exception.

throws The "throws" keyword is used to declare exceptions. It doesn't throw an


exception. It specifies that there may occur an exception in the method. It is
always used with method signature.

Common Scenarios of Java Exceptions


There are given some scenarios where unchecked exceptions may occur. They are as
follows:

1) A scenario where ArithmeticException occurs

If we divide any number by zero, there occurs an ArithmeticException.

1. int a=50/0;//ArithmeticException

2) A scenario where NullPointerException occurs

If we have a null value in any variable, performing any operation on the variable throws a
NullPointerException.

1. String s=null;
2. System.out.println(s.length());//NullPointerException

3) A scenario where NumberFormatException occurs

The wrong formatting of any value may occur NumberFormatException. Suppose I have a
string variable that has characters, converting this variable into digit will occur
NumberFormatException.

1. String s="abc";
2. int i=Integer.parseInt(s);//NumberFormatException

4) A scenario where ArrayIndexOutOfBoundsException occurs

If you are inserting any value in the wrong index, it would result in
ArrayIndexOutOfBoundsException as shown below:

1. int a[]=new int[5];


2. a[10]=50; //ArrayIndexOutOfBoundsException

There are two types of exceptions: checked exception and unchecked


exception. In this guide, we will discuss them. The main difference between
checked and unchecked exception is that the checked exceptions are
checked at compile-time while unchecked exceptions are checked at runtime.

What are checked exceptions?


Checked exceptions are checked at compile-time. It means if a method is
throwing a checked exception then it should handle the exception using try-
catch block or it should declare the exception using throws keyword, otherwise
the program will give a compilation error.

Checked: are the exceptions that are checked at compile time. If some code
within a method throws a checked exception, then the method must either
handle the exception or it must specify the exception using throws keyword.

What are Unchecked exceptions?


Unchecked exceptions are not checked at compile time. It means if your
program is throwing an unchecked exception and even if you didn’t
handle/declare that exception, the program won’t give a compilation error.
Most of the times these exception occurs due to the bad data provided by user
during the user-program interaction. It is up to the programmer to judge the
conditions in advance, that can cause such exceptions and handle them
appropriately. All Unchecked exceptions are direct sub classes
of RuntimeException class.

Unchecked are the exceptions that are not checked at compiled time. In C++,
all exceptions are unchecked, so it is not forced by the compiler to either
handle or specify the exception. It is up to the programmers to be civilized,
and specify or catch the exceptions.
In Java exceptions under Error and RuntimeException classes are unchecked
exceptions, everything else under throwable is checked.

What are different types of exceptions


in Java?
Introduction
An exception is an event that occurs during the execution of a program that
disrupts the normal flow of the program's instructions. In Java, exceptions are
objects that describe an exceptional (error) condition that has occurred in a
piece of code. They are used to handle errors and other exceptional events in
programs written in the Java programming language.
Types of exceptions
There are two kinds of exceptions in Java:
1. Checked exceptions: These are the exceptions that are checked by
the compiler at compile time. If a method throws a checked exception,
then the caller of the method must either handle the exception or
declare it in the throws clause.
2. Unchecked exceptions: These are the exceptions that are not
checked by the compiler at compile time. They include runtime
exceptions and errors.

The Throwable class is the superclass of all exceptions and errors in Java. All
other exception classes are subclasses of the Throwable.
Checked exceptions
The table given below represents some of the commonly used checked
exceptions in Java with their description.
List of common checked exceptions in Java
Exception class Description

IOException This exception is raised when an input/output


operation fails

SQLException This exception is raised when a database operation


fails

ClassNotFoundExceptio This exception is raised when a class cannot be


n found.

InstantiationException This exception is raised when an object cannot be


instantiated.

NoSuchMethodExceptio This exception is raised when a method cannot be


n found.
Unchecked exceptions
The RuntimeException class is the superclass of all unchecked exceptions.
The table below represents some of the commonly used unchecked exception
classes in Java with their description.
List of common unchecked exceptions in Java
Exception classes Description

RuntimeException This is the superclass of all unchecked


exceptions.

NullPointerException: This exception is raised when a null value


is used where an object is required.

ArithmeticException This exception is raised when an


arithmetic operation fails.

ArrayIndexOutOfBoundsException This exception is raised when an array


index is out of bounds.

IllegalArgumentException This exception is raised when an illegal


argument is used.

IllegalStateException This exception is raised when an illegal


state is detected.

ConcurrentModificationException This exception is raised when a collection


is modified while it is being iterated over.

You might also like