4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
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.
Exception Handling in Java - Javatpoint
Advantage of Exception Handling
https://www.javatpoint.com/exception-handling-in-java 1/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
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:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
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.
Do You Know?
https://www.javatpoint.com/exception-handling-in-java 2/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
What is the difference between checked and unchecked
exceptions?
What happens behind the code int data=50/0;?
Why use multiple catch block?
Is there any possibility when finally block is not executed?
What is exception propagation?
What is the difference between throw and throws keyword?
What are the 4 rules for using exception handling with
method overriding?
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:
https://www.javatpoint.com/exception-handling-in-java 3/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
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
https://www.javatpoint.com/exception-handling-in-java 4/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
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.
https://www.javatpoint.com/exception-handling-in-java 5/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
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.
⇧
https://www.javatpoint.com/exception-handling-in-java 6/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
Java Exception Handling Example
Let's see an example of Java Exception Handling where we using a
try-catch statement to handle the exception.
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
Test it Now
Output:
Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...
In the above example, 100/0 raises an ArithmeticException which is
handled by a try-catch block.
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.
int a=50/0;//ArithmeticException
https://www.javatpoint.com/exception-handling-in-java 7/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
2) A scenario where NullPointerException occurs
If we have a null value in any variable, performing any operation on
the variable throws a NullPointerException.
String s=null;
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.
String s="abc";
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:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Java Exceptions Index
1. Java Try-Catch Block
2. Java Multiple Catch Block
3. Java Nested Try
4. Java Finally Block
5. Java Throw Keyword
6. Java Exception Propagation
7. Java Throws Keyword
⇧
https://www.javatpoint.com/exception-handling-in-java 8/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
8. Java Throw vs Throws
9. Java Final vs Finally vs Finalize
10. Java Exception Handling with Method Overriding
11. Java Custom Exceptions
← Prev Next →
Youtube For Videos Join Our Youtube Channel:
Join Now
Help Others, Please Share
Learn Latest Tutorials
SoapUI RPA tutorial manual testing
tutorial tutorial
RPA
SoapUI Manual T.
cucumber Appium postgresql
tutorial tutorial tutorial ⇧
https://www.javatpoint.com/exception-handling-in-java 9/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
Cucumber Appium PostgreSQL
Apache Solr MongoDB Gimp Tutorial
Tutorial tutorial
Gimp
Solr MongoDB
Verilog Teradata PhoneGap
Tutorial Tutorial Tutorial
Verilog Teradata PhoneGap
Preparation
Aptitude Logical Verbal Ability
Reasoning
Aptitude Verbal A.
Reasoning
Interview Company
Questions Interview
Questions
Interview
Company
Trending Technologies
Artificial AWS Tutorial Selenium
Intelligence tutorial
Tutorial AWS
Selenium
AI
Cloud tutorial Hadoop ReactJS
tutorial Tutorial
Cloud
Hadoop ReactJS
⇧
https://www.javatpoint.com/exception-handling-in-java 10/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
Data Science Angular 7 Blockchain
Tutorial Tutorial Tutorial
D. Science Angular 7 Blockchain
Git Tutorial Machine DevOps
Learning Tutorial Tutorial
Git
ML DevOps
B.Tech / MCA
DBMS tutorial Data DAA tutorial
Structures
DBMS tutorial DAA
DS
Operating Computer Compiler
System tutorial Network tutorial Design tutorial
OS C. Network Compiler D.
Computer Discrete Ethical
Organization and Mathematics Hacking Tutorial
Architecture Tutorial
E. Hacking
COA D. Math.
Computer Software html tutorial
Graphics Tutorial Engineering
Tutorial Web Tech.
C. Graphics
Software E.
Cyber Automata C Language
Security tutorial Tutorial tutorial
⇧
Cyber Sec. Automata C
https://www.javatpoint.com/exception-handling-in-java 11/12
4/26/2021 Exception Handling in Java | Java Exceptions - javatpoint
C++ tutorial Java tutorial .Net
Framework
C++ Java tutorial
.Net
Python tutorial List of Control
Programs Systems tutorial
Python
Programs Control S.
Data Mining
Tutorial
Data Mining
https://www.javatpoint.com/exception-handling-in-java 12/12