SlideShare a Scribd company logo
Core Java Training
Exception Handling
Page 1Classification: Restricted
Agenda
• Exception Handling
• Exception Class hierarchy
• Types of Exception
• Keywords for Exception Handling
Page 2Classification: Restricted
Exceptions…
• One of the powerful mechanism to handle the runtime errors so that
normal flow of the application can be maintained.
• In Java, exception is an event that disrupts the normal flow of the program.
It is an object which is thrown at runtime.
• Exception Handling is a mechanism to handle runtime errors such as
ClassNotFound, IO, SQL, Remote etc.
Page 3Classification: Restricted
Exception Class Hierarchy
Page 4Classification: Restricted
Types of Exception
Page 5Classification: Restricted
Types of Exception
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are
known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions
are checked at compile-time.
The programmer is compelled to handle (caught or declared) these in his code.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time rather they are checked at
runtime. These are exceptions that the programmer is not compelled to handle in his
code.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError
etc.
Page 6Classification: Restricted
Examples
• ArithmeticException
int a=50/0;//ArithmeticException
• NullPointerException
String s=null;
System.out.println(s.length());//NullPointerException
• NumberFormatException
String s="abc";
int i=Integer.parseInt(s);//NumberFormatException
• ArrayIndexOutOfBoundsException
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Page 7Classification: Restricted
Keywords for Exception Handling
• try
• catch
• finally
• throw
• throws
Page 8Classification: Restricted
Problem without exception handling
public class Testtrycatch1{
public static void main(String args[]){
int data=50/0;//may throw exception
System.out.println("rest of the code...");
}
}
Page 9Classification: Restricted
Internal working…
Page 10Classification: Restricted
Solution by exception handling
public class Testtrycatch2{
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...");
}
}
Page 11Classification: Restricted
Example…
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e){System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");}
catch(Exception e){System.out.println("common task completed");}
System.out.println("rest of the code...");
}
}
Rule: At a time only one Exception occurs and at a time only one catch block is executed.
Rule: All catch blocks must be ordered from most specific to most general i.e. catch for
ArithmeticException must come before catch for Exception.
Page 12Classification: Restricted
Nested try catch blocks…
class Excep6{
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..");
}
}
Page 13Classification: Restricted
finally block
• Java finally block is a block that is used to execute important code
(releasing resources) such as closing connection, stream etc.
• Java finally block is always executed whether exception is handled or not.
• Java finally block follows try or catch block.
Page 14Classification: Restricted
Examples…
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
Page 15Classification: Restricted
Example: Exception occurs, not handled…but finally is executed.
class TestFinallyBlock1{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
Page 16Classification: Restricted
Example: Exception occurs and handled
public class TestFinallyBlock2{
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...");
}
}
Page 17Classification: Restricted
More rules…
• Rule: For each try block there can be zero or more catch blocks, but only
one finally block.
• Note: The finally block will not be executed if program exits(either by
calling System.exit() or by causing a fatal error that causes the process to
abort)
Page 18Classification: Restricted
Topics to be covered in next session
• throw and throws keywords
• Exception propagation – the cases of Checked and Unchecked Exceptions
• Defining your own custom Exception
Page 19Classification: Restricted
Thank you!

More Related Content

PPSX
Elements of Java Language - Continued
PPSX
Exception Handling - Continued
PPSX
OOP with Java - Abstract Classes and Interfaces
PPS
Control statements
PPT
06 exceptions
PPS
Java Exception handling
PPSX
OOP with Java - Continued
PPTX
Java Reflection @KonaTechAdda
Elements of Java Language - Continued
Exception Handling - Continued
OOP with Java - Abstract Classes and Interfaces
Control statements
06 exceptions
Java Exception handling
OOP with Java - Continued
Java Reflection @KonaTechAdda

What's hot (19)

PPTX
Session 11 - OOP's with Java - Packaging and Access Modifiers
PPTX
L14 exception handling
PPSX
OOP with Java - Part 3
PDF
Functional Java 8 - Introduction
PPTX
Control structures in java
PPTX
Exception handling
PDF
Exception Handling
PPTX
Nalinee java
PDF
Java review: try catch
PPT
Exception handler
PPTX
JAVA - Throwable class
PPT
Multi catch statement
PPTX
Pi j1.3 operators
PPTX
Lecture - 5 Control Statement
DOCX
What is an exception in java?
PDF
Introduction to JAVA
PPS
Exception handling in c programming
PDF
Java SE 9 modules - an introduction (July 2018)
PPT
Exception handling in c++ by manoj vasava
Session 11 - OOP's with Java - Packaging and Access Modifiers
L14 exception handling
OOP with Java - Part 3
Functional Java 8 - Introduction
Control structures in java
Exception handling
Exception Handling
Nalinee java
Java review: try catch
Exception handler
JAVA - Throwable class
Multi catch statement
Pi j1.3 operators
Lecture - 5 Control Statement
What is an exception in java?
Introduction to JAVA
Exception handling in c programming
Java SE 9 modules - an introduction (July 2018)
Exception handling in c++ by manoj vasava
Ad

Similar to Exception Handling - Part 1 (20)

PPTX
Session 12 - Exception Handling - Part 1
PPTX
Java exception handling
PPTX
Exception Handling In Java Presentation. 2024
PPTX
Java-Exception Handling Presentation. 2024
PPTX
Exception handling in java
DOCX
Exception handling in java
PPTX
java exception.pptx
PPTX
OBJECT ORIENTED PROGRAMMING_Unit3_NOTES first half.pptx
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
PPTX
UNIT-3.pptx Exception Handling and Multithreading
PDF
Chapter 5 Exception Handling (1).pdf
PPTX
Exception handling in java
PPT
Exceptions in java
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
PPT
A36519192_21789_4_2018_Exception Handling.ppt
PPTX
Exception handling
PPTX
UNIT 2.pptx
PPTX
Exception handling in java
Session 12 - Exception Handling - Part 1
Java exception handling
Exception Handling In Java Presentation. 2024
Java-Exception Handling Presentation. 2024
Exception handling in java
Exception handling in java
java exception.pptx
OBJECT ORIENTED PROGRAMMING_Unit3_NOTES first half.pptx
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
UNIT-3.pptx Exception Handling and Multithreading
Chapter 5 Exception Handling (1).pdf
Exception handling in java
Exceptions in java
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
A36519192_21789_4_2018_Exception Handling.ppt
Exception handling
UNIT 2.pptx
Exception handling in java
Ad

More from Hitesh-Java (20)

PPSX
Spring - Part 4 - Spring MVC
PPSX
Spring - Part 3 - AOP
PPSX
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
PPSX
Spring - Part 1 - IoC, Di and Beans
PPSX
JSP - Part 2 (Final)
PPSX
JSP - Part 1
PPSX
Struts 2 - Hibernate Integration
PPSX
Struts 2 - Introduction
PPSX
Hibernate - Part 2
PPSX
Hibernate - Part 1
PPSX
JDBC Part - 2
PPSX
PPSX
Java IO, Serialization
PPSX
Inner Classes
PPSX
Collections - Maps
PPSX
Review Session - Part -2
PPSX
Review Session and Attending Java Interviews
PPSX
Collections - Lists, Sets
PPSX
Collections - Sorting, Comparing Basics
PPSX
Collections - Array List
Spring - Part 4 - Spring MVC
Spring - Part 3 - AOP
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 1 - IoC, Di and Beans
JSP - Part 2 (Final)
JSP - Part 1
Struts 2 - Hibernate Integration
Struts 2 - Introduction
Hibernate - Part 2
Hibernate - Part 1
JDBC Part - 2
Java IO, Serialization
Inner Classes
Collections - Maps
Review Session - Part -2
Review Session and Attending Java Interviews
Collections - Lists, Sets
Collections - Sorting, Comparing Basics
Collections - Array List

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Electronic commerce courselecture one. Pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
Teaching material agriculture food technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Approach and Philosophy of On baking technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx
Empathic Computing: Creating Shared Understanding
Electronic commerce courselecture one. Pdf
Review of recent advances in non-invasive hemoglobin estimation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Spectroscopy.pptx food analysis technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Teaching material agriculture food technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Approach and Philosophy of On baking technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx

Exception Handling - Part 1

  • 2. Page 1Classification: Restricted Agenda • Exception Handling • Exception Class hierarchy • Types of Exception • Keywords for Exception Handling
  • 3. Page 2Classification: Restricted Exceptions… • One of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained. • In Java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. • Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.
  • 6. Page 5Classification: Restricted Types of Exception 1) Checked Exception The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time. The programmer is compelled to handle (caught or declared) these in his code. 2) Unchecked Exception The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime. These are exceptions that the programmer is not compelled to handle in his code. 3) Error Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
  • 7. Page 6Classification: Restricted Examples • ArithmeticException int a=50/0;//ArithmeticException • NullPointerException String s=null; System.out.println(s.length());//NullPointerException • NumberFormatException String s="abc"; int i=Integer.parseInt(s);//NumberFormatException • ArrayIndexOutOfBoundsException int a[]=new int[5]; a[10]=50; //ArrayIndexOutOfBoundsException
  • 8. Page 7Classification: Restricted Keywords for Exception Handling • try • catch • finally • throw • throws
  • 9. Page 8Classification: Restricted Problem without exception handling public class Testtrycatch1{ public static void main(String args[]){ int data=50/0;//may throw exception System.out.println("rest of the code..."); } }
  • 11. Page 10Classification: Restricted Solution by exception handling public class Testtrycatch2{ 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..."); } }
  • 12. Page 11Classification: Restricted Example… public class TestMultipleCatchBlock{ public static void main(String args[]){ try{ int a[]=new int[5]; a[5]=30/0; } catch(ArithmeticException e){System.out.println("task1 is completed");} catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");} catch(Exception e){System.out.println("common task completed");} System.out.println("rest of the code..."); } } Rule: At a time only one Exception occurs and at a time only one catch block is executed. Rule: All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception.
  • 13. Page 12Classification: Restricted Nested try catch blocks… class Excep6{ 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.."); } }
  • 14. Page 13Classification: Restricted finally block • Java finally block is a block that is used to execute important code (releasing resources) such as closing connection, stream etc. • Java finally block is always executed whether exception is handled or not. • Java finally block follows try or catch block.
  • 15. Page 14Classification: Restricted Examples… class TestFinallyBlock{ public static void main(String args[]){ try{ int data=25/5; System.out.println(data); } catch(NullPointerException e){System.out.println(e);} finally{System.out.println("finally block is always executed");} System.out.println("rest of the code..."); } }
  • 16. Page 15Classification: Restricted Example: Exception occurs, not handled…but finally is executed. class TestFinallyBlock1{ public static void main(String args[]){ try{ int data=25/0; System.out.println(data); } catch(NullPointerException e){System.out.println(e);} finally{System.out.println("finally block is always executed");} System.out.println("rest of the code..."); } }
  • 17. Page 16Classification: Restricted Example: Exception occurs and handled public class TestFinallyBlock2{ 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..."); } }
  • 18. Page 17Classification: Restricted More rules… • Rule: For each try block there can be zero or more catch blocks, but only one finally block. • Note: The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort)
  • 19. Page 18Classification: Restricted Topics to be covered in next session • throw and throws keywords • Exception propagation – the cases of Checked and Unchecked Exceptions • Defining your own custom Exception