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

1.Exception Handling in Java | Java Exceptions - Javatpoint

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

1.Exception Handling in Java | Java Exceptions - Javatpoint

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

Home Java Programs OOPs String Exception

ADVERTISEMENT

Exception Handling in Java


ADVERTISEMENT

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

In this tutorial, we will learn about Java exceptions, it's types, and the difference
between checked and unchecked exceptions.

What is Exception in Java?

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 1 of 16
:
Dictionary Meaning: Exception is an abnormal condition.

ADVERTISEMENT

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

ADVERTISEMENT

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 2 of 16
:
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 need to handle exceptions. Let's consider 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;

ADVERTISEMENT

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 3 of 16
:
Suppose there are 10 statements in a Java program and an exception occurs at
statement 5; the rest of the code will not be executed, i.e., statements 6 to 10 will
not be executed. However, when we perform exception handling, the rest of the
statements will be executed. That is why we use exception handling in Java.

Do You Know?

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 the finally block is not executed?

What is exception propagation?

What is the difference between the throw and throws keyword?

What are the 4 rules for using exception handling with method
overriding?

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 4 of 16
:
Hierarchy of Java Exception classes
The java.lang.Throwable class is the root class of Java Exception hierarchy inherited
by two subclasses: Exception and Error. The hierarchy of Java Exception classes is
given below:

Types of Java Exceptions

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 5 of 16
:
There are mainly two types of exceptions: checked and unchecked. An error is
considered as the unchecked exception. However, according to Oracle, there are
three types of exceptions namely:

1. Checked Exception

2. Unchecked Exception

3. Error

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 6 of 16
:
Difference between Checked and Unchecked
Exceptions

1) Checked Exception

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 7 of 16
:
The classes that directly inherit the Throwable class except RuntimeException and
Error are known as checked exceptions. For example, IOException, SQLException,
etc. Checked exceptions are checked at compile-time.

2) Unchecked Exception

The classes that inherit the RuntimeException are known as unchecked exceptions.
For example, ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException, etc. Unchecked exceptions are not checked at
compile-time, but they are checked at runtime.

3) Error

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 8 of 16
:
Error is irrecoverable. Some example of errors are OutOfMemoryError,
VirtualMachineError, AssertionError etc.

Java Exception Keywords


Java provides five keywords that are used to handle the exception. The following
table describes each.

Keyword Description

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

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 necessary 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 specifies that


there may occur an exception in the method. It doesn't throw an
exception. It is always used with method signature.

Java Exception Handling Example


Let's see an example of Java Exception Handling in which we are using a try-catch
statement to handle the exception.

JavaExceptionExample.java

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 9 of 16
:
1. public class JavaExceptionExample{
2. public static void main(String args[]){
3. try{
4. //code that may raise exception
5. int data=100/0;
6. }catch(ArithmeticException e){System.out.println(e);}
7. //rest code of the program
8. System.out.println("rest of the code...");
9. }
10. }

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.

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

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 10 of 16
:
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

If the formatting of any variable or number is mismatched, it may result into


NumberFormatException. Suppose we have a string variable that has characters;
converting this variable into digit will cause NumberFormatException.

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

4) A scenario where ArrayIndexOutOfBoundsException occurs

When an array exceeds to it's size, the ArrayIndexOutOfBoundsException occurs.


there may be other reasons to occur ArrayIndexOutOfBoundsException. Consider
the following statements.

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


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

Java Exceptions Index


1. Java Try-Catch Block

2. Java Multiple Catch Block

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 11 of 16
:
3. Java Nested Try

4. Java Finally Block

5. Java Throw Keyword

6. Java Exception Propagation

7. Java Throws Keyword

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

Feedback

Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

Learn Latest Tutorials

Splunk tutorial SPSS tutorial Swagger tutorial

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 12 of 16
:
Splunk tutorial SPSS tutorial Swagger tutorial

Splunk SPSS Swagger

T-SQL tutorial Tumblr tutorial React tutorial

Transact-SQL Tumblr ReactJS

Regex tutorial

Regex Reinforcement R Programming


Learning

RxJS tutorial

RxJS React Native Python Design


Patterns

Keras tutorial

Python Pillow Python Turtle Keras

Preparation

Aptitude Verbal Ability

Aptitude Reasoning Verbal Ability

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 13 of 16
:
Interview Questions Company Questions

Trending Technologies

AWS Tutorial Selenium tutorial

Artificial AWS Selenium


Intelligence

Cloud Computing Hadoop tutorial ReactJS Tutorial

Cloud Computing Hadoop ReactJS

Data Science Angular 7 Blockchain

Git Tutorial DevOps Tutorial

Git Machine Learning DevOps

B.Tech / MCA

DBMS tutorial DAA tutorial

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 14 of 16
:
DBMS tutorial DAA tutorial

DBMS Data Structures DAA

Operating System

Operating System Computer Network Compiler Design

Ethical Hacking

Computer Discrete Ethical Hacking


Organization Mathematics

html tutorial

Computer Graphics Software Web Technology


Engineering

Automata Tutorial

Cyber Security Automata C Programming

C++ tutorial Java tutorial

C++ Java .Net

Python tutorial List of Programs

Python Programs Control System

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 15 of 16
:
Data Mining Data Warehouse

https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 16 of 16
:

You might also like