SlideShare a Scribd company logo
Exception HandlingException Handling
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and Data StructuresJava and Data Structures
Introduction
• An exception is a condition that is caused by a run-time error in
the program.
– Example: Dividing integer by zero. ( / by zero)
• Java has special mechanism for handling such types of errors.
• Its Exception handling.
• Core advantage of exception handling is to maintain the normal
flow of the application.
• Mechanism performs following tasks:
– Find the problem (Hit the exception)
– Inform that error has occur(Throw the exception)
– Receive the error information(catch the exception)
– Take a correct action (Handle the exception)
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Exceptions
Categorized into two types:
• Checked exception:
– Explicitly handle in the code itself with the help of try-catch block.
– Extended from java.lang.Exception class
– e.g.IOException, SQLException etc.
• Unchecked exception:
– Not handle in the program code .
– JVM handles such exceptions.
– Extended from the java.lang.RuntimeException
– e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Common Java Exceptions
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Exception Description
ClassNotFoundException Class not found.
NoSuchFieldException A requested field does not exist.
NoSuchMethodException A requested method does not exist.
Exception Description
Common Java Exceptions
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Exception Description
ArithmeticException Arithmetic error, such as divide-by-zero.
ArrayIndexOutOfBoundsException Array index is out-of-bounds.
ArrayStoreException
Assignment to an array element of an
incompatible type.
ClassCastException Invalid cast.
IndexOutOfBoundsException Some type of index is out-of-bounds.
NullPointerException Invalid use of a null reference.
NumberFormatException
Invalid conversion of a string to a
numeric format.
StringIndexOutOfBounds
Attempt to index outside the bounds of
a string.
try and catch block
• Enclose the code that might throw an exception in try block.
• It must be used within the method and must be followed by
either catch or finally block.
• Syntax of try with catch block
• Syntax of try with finally block
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
try{  
...  
}catch(Exception_class_Name reference)
{}  
try{  
...  
}finally
{}  
try and catch block
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Testtrycatch
{
public static void main(String args[])
{
int data = 50/0;
System.out.println("rest of the code...");
}
}
Output:Exception in thread main java.lang.ArithmeticException:/ by zero
try and catch block
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Testtrycatch
{
public static void main(String args[])
{
try{
int data=50/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code...");
}
}
Output:Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...
Multiple Catch blocks
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class MultipleCatch
{
public static void main(String argv[])
{
int num1 = 10;
int num2 = 0;
int result = 0;
int arr[] = new int[5];
try
{
arr[0] = 0;
arr[1] = 1;
arr[2] = 2;
arr[3] = 3;
arr[4] = 4;
arr[5] = 5;
result = num1 / num2;
System.out.println("Result of Division : " + result);
}catch (ArithmeticException e) {
System.out.println("Err: Divided by Zero");
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Err: Array Out of Bound");
}
}
}
Nested try…block
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class NestedTryBlock
{
public static void main(String args[])
{
try{
try{
System.out.println("going to divide");
int b =39/0;
}catch(ArithmeticException e){
System.out.println(e);
}
try{
int a[]=new int[5];
a[5]=4;
}catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);}
System.out.println("other statement);
}catch(Exception e){System.out.println("handeled");}
System.out.println("normal flow..");
}
}
finally
• The finally block is a block that is always executed.
• It is mainly used to perform some important tasks such
as closing connection, stream etc.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
finally
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class FinallyBlock
{
public static void main(String args[])
{
try{
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e){
System.out.println(e);
}
finally{
System.out.println("finally block is
always executed");
}
System.out.println("rest of the code...");
}
}
Output: Exception in thread main java.lang.ArithmeticException:/ by zero
finally block is always executed
rest of the code...
throw
• The throw keyword is used to explicitly throw an
exception.
• We can throw either checked or uncheked exception.
• The throw keyword is mainly used to throw custom
exception.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
throws
throws clause is used to handle the exception thrown by methods
in program.
void method_name() throws exception_class_name
{  
 ...   
}  
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Q & A

More Related Content

PPT
1. Overview of Java
PPT
7. Multithreading
PPT
5. Inheritances, Packages and Intefaces
PPT
2. Basics of Java
PPT
4. Classes and Methods
PPT
9. Input Output in java
PPT
8. String
PPTX
Static keyword ppt
1. Overview of Java
7. Multithreading
5. Inheritances, Packages and Intefaces
2. Basics of Java
4. Classes and Methods
9. Input Output in java
8. String
Static keyword ppt

What's hot (8)

PDF
Keywords and classes
PPTX
6. static keyword
PDF
Methods and constructors
PPT
Corejava Training in Bangalore Tutorial
PDF
PPT
Java static keyword
PDF
L2 datatypes and variables
PDF
Java keywords
Keywords and classes
6. static keyword
Methods and constructors
Corejava Training in Bangalore Tutorial
Java static keyword
L2 datatypes and variables
Java keywords
Ad

Viewers also liked (7)

PDF
Exception handling
PPT
14. Linked List
PPT
3. Data types and Variables
PPT
11. Arrays
PPT
10. Introduction to Datastructure
PPT
13. Queue
PPT
12. Stack
Exception handling
14. Linked List
3. Data types and Variables
11. Arrays
10. Introduction to Datastructure
13. Queue
12. Stack
Ad

Similar to 6. Exception Handling (20)

PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
PDF
JAVA PROGRAMMING- Exception handling - Multithreading
PPTX
Exception handling2.0.pptx
DOCX
What is an exception in java?
PPT
Exceptions
PPTX
Exception handling in java
PPTX
Exception handling
PPT
06 exceptions
PPTX
25 java interview questions
PDF
Chapter 5 Exception Handling (1).pdf
PPTX
PROGRAMMING IN JAVA
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU1.pptx
PPTX
Nalinee java
PPTX
Session 38 - Core Java (New Features) - Part 1
PDF
Top 50 Java Interviews Questions | Tutort Academy - Course for Working Profes...
PPTX
Exception handling in java
PPT
Exceptions in java
PPTX
Exceptions handling in java
PPTX
UNIT 2.pptx
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
JAVA PROGRAMMING- Exception handling - Multithreading
Exception handling2.0.pptx
What is an exception in java?
Exceptions
Exception handling in java
Exception handling
06 exceptions
25 java interview questions
Chapter 5 Exception Handling (1).pdf
PROGRAMMING IN JAVA
OBJECT ORIENTED PROGRAMMING STRUCU1.pptx
Nalinee java
Session 38 - Core Java (New Features) - Part 1
Top 50 Java Interviews Questions | Tutort Academy - Course for Working Profes...
Exception handling in java
Exceptions in java
Exceptions handling in java
UNIT 2.pptx

More from Nilesh Dalvi (12)

PPT
Standard Template Library
PPT
Templates
PPT
File handling
PPT
Input and output in C++
PPT
Strings
PPT
Polymorphism
PPT
Inheritance : Extending Classes
PPT
Operator Overloading
PDF
Constructors and destructors
PDF
Classes and objects
PDF
Introduction to cpp
PDF
Introduction to oops concepts
Standard Template Library
Templates
File handling
Input and output in C++
Strings
Polymorphism
Inheritance : Extending Classes
Operator Overloading
Constructors and destructors
Classes and objects
Introduction to cpp
Introduction to oops concepts

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Classroom Observation Tools for Teachers
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PPTX
GDM (1) (1).pptx small presentation for students
PPH.pptx obstetrics and gynecology in nursing
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
human mycosis Human fungal infections are called human mycosis..pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Abdominal Access Techniques with Prof. Dr. R K Mishra
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf
Computing-Curriculum for Schools in Ghana
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Sports Quiz easy sports quiz sports quiz
Classroom Observation Tools for Teachers
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
GDM (1) (1).pptx small presentation for students

6. Exception Handling

  • 1. Exception HandlingException Handling By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and Data StructuresJava and Data Structures
  • 2. Introduction • An exception is a condition that is caused by a run-time error in the program. – Example: Dividing integer by zero. ( / by zero) • Java has special mechanism for handling such types of errors. • Its Exception handling. • Core advantage of exception handling is to maintain the normal flow of the application. • Mechanism performs following tasks: – Find the problem (Hit the exception) – Inform that error has occur(Throw the exception) – Receive the error information(catch the exception) – Take a correct action (Handle the exception) Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3. Exceptions Categorized into two types: • Checked exception: – Explicitly handle in the code itself with the help of try-catch block. – Extended from java.lang.Exception class – e.g.IOException, SQLException etc. • Unchecked exception: – Not handle in the program code . – JVM handles such exceptions. – Extended from the java.lang.RuntimeException – e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. Common Java Exceptions Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Exception Description ClassNotFoundException Class not found. NoSuchFieldException A requested field does not exist. NoSuchMethodException A requested method does not exist. Exception Description
  • 5. Common Java Exceptions Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Exception Description ArithmeticException Arithmetic error, such as divide-by-zero. ArrayIndexOutOfBoundsException Array index is out-of-bounds. ArrayStoreException Assignment to an array element of an incompatible type. ClassCastException Invalid cast. IndexOutOfBoundsException Some type of index is out-of-bounds. NullPointerException Invalid use of a null reference. NumberFormatException Invalid conversion of a string to a numeric format. StringIndexOutOfBounds Attempt to index outside the bounds of a string.
  • 6. try and catch block • Enclose the code that might throw an exception in try block. • It must be used within the method and must be followed by either catch or finally block. • Syntax of try with catch block • Syntax of try with finally block Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). try{   ...   }catch(Exception_class_Name reference) {}   try{   ...   }finally {}  
  • 7. try and catch block Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Testtrycatch { public static void main(String args[]) { int data = 50/0; System.out.println("rest of the code..."); } } Output:Exception in thread main java.lang.ArithmeticException:/ by zero
  • 8. try and catch block Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Testtrycatch { public static void main(String args[]) { try{ int data=50/0; } catch(ArithmeticException e) { System.out.println(e); } System.out.println("rest of the code..."); } } Output:Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...
  • 9. Multiple Catch blocks Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class MultipleCatch { public static void main(String argv[]) { int num1 = 10; int num2 = 0; int result = 0; int arr[] = new int[5]; try { arr[0] = 0; arr[1] = 1; arr[2] = 2; arr[3] = 3; arr[4] = 4; arr[5] = 5; result = num1 / num2; System.out.println("Result of Division : " + result); }catch (ArithmeticException e) { System.out.println("Err: Divided by Zero"); }catch (ArrayIndexOutOfBoundsException e) { System.out.println("Err: Array Out of Bound"); } } }
  • 10. Nested try…block Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class NestedTryBlock { public static void main(String args[]) { try{ try{ System.out.println("going to divide"); int b =39/0; }catch(ArithmeticException e){ System.out.println(e); } try{ int a[]=new int[5]; a[5]=4; }catch(ArrayIndexOutOfBoundsException e) { System.out.println(e);} System.out.println("other statement); }catch(Exception e){System.out.println("handeled");} System.out.println("normal flow.."); } }
  • 11. finally • The finally block is a block that is always executed. • It is mainly used to perform some important tasks such as closing connection, stream etc. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 12. finally Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class FinallyBlock { public static void main(String args[]) { try{ int data=25/0; System.out.println(data); } catch(ArithmeticException e){ System.out.println(e); } finally{ System.out.println("finally block is always executed"); } System.out.println("rest of the code..."); } } Output: Exception in thread main java.lang.ArithmeticException:/ by zero finally block is always executed rest of the code...
  • 13. throw • The throw keyword is used to explicitly throw an exception. • We can throw either checked or uncheked exception. • The throw keyword is mainly used to throw custom exception. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 14. throws throws clause is used to handle the exception thrown by methods in program. void method_name() throws exception_class_name {    ...    }   Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 15. Q & A