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

What's hot (19)

PPTX
Session 11 - OOP's with Java - Packaging and Access Modifiers
PawanMM
 
PPTX
L14 exception handling
teach4uin
 
PPSX
OOP with Java - Part 3
Hitesh-Java
 
PDF
Functional Java 8 - Introduction
Łukasz Biały
 
PPTX
Control structures in java
VINOTH R
 
PPTX
Exception handling
Minal Maniar
 
PDF
Exception Handling
Alpesh Oza
 
PPTX
Nalinee java
Nalinee Choudhary
 
PDF
Java review: try catch
Muzahidul Islam
 
PPT
Exception handler
dishni
 
PPTX
JAVA - Throwable class
asifpatel20
 
PPT
Multi catch statement
myrajendra
 
PPTX
Pi j1.3 operators
mcollison
 
PPTX
Lecture - 5 Control Statement
manish kumar
 
DOCX
What is an exception in java?
Pramod Yadav
 
PDF
Introduction to JAVA
Professional Guru
 
PPS
Exception handling in c programming
Raza Najam
 
PDF
Java SE 9 modules - an introduction (July 2018)
Stephen Colebourne
 
PPT
Exception handling in c++ by manoj vasava
Manoj_vasava
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
PawanMM
 
L14 exception handling
teach4uin
 
OOP with Java - Part 3
Hitesh-Java
 
Functional Java 8 - Introduction
Łukasz Biały
 
Control structures in java
VINOTH R
 
Exception handling
Minal Maniar
 
Exception Handling
Alpesh Oza
 
Nalinee java
Nalinee Choudhary
 
Java review: try catch
Muzahidul Islam
 
Exception handler
dishni
 
JAVA - Throwable class
asifpatel20
 
Multi catch statement
myrajendra
 
Pi j1.3 operators
mcollison
 
Lecture - 5 Control Statement
manish kumar
 
What is an exception in java?
Pramod Yadav
 
Introduction to JAVA
Professional Guru
 
Exception handling in c programming
Raza Najam
 
Java SE 9 modules - an introduction (July 2018)
Stephen Colebourne
 
Exception handling in c++ by manoj vasava
Manoj_vasava
 

Similar to Exception Handling - Part 1 (20)

PPTX
Session 12 - Exception Handling - Part 1
PawanMM
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPTX
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
PPTX
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
PPTX
Exception handling in java
Lovely Professional University
 
DOCX
Exception handling in java
gopalrajput11
 
PPTX
java exception.pptx
SukhpreetSingh519414
 
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PPTX
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
PDF
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
PPTX
Exception handling in java
ARAFAT ISLAM
 
PPT
Exceptions in java
JasmeetSingh326
 
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
roshansingh0407
 
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
roshansingh0407
 
PPT
A36519192_21789_4_2018_Exception Handling.ppt
promila09
 
PPTX
Exception handling
Ardhendu Nandi
 
PPTX
UNIT 2.pptx
EduclentMegasoftel
 
PPTX
Exception handling in java
pooja kumari
 
PPTX
Exception handling in java
pooja kumari
 
Session 12 - Exception Handling - Part 1
PawanMM
 
Java exception handling
BHUVIJAYAVELU
 
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Exception handling in java
Lovely Professional University
 
Exception handling in java
gopalrajput11
 
java exception.pptx
SukhpreetSingh519414
 
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
Exception handling in java
ARAFAT ISLAM
 
Exceptions in java
JasmeetSingh326
 
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
roshansingh0407
 
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
roshansingh0407
 
A36519192_21789_4_2018_Exception Handling.ppt
promila09
 
Exception handling
Ardhendu Nandi
 
UNIT 2.pptx
EduclentMegasoftel
 
Exception handling in java
pooja kumari
 
Exception handling in java
pooja kumari
 
Ad

More from Hitesh-Java (20)

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

Recently uploaded (20)

PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Français Patch Tuesday - Juillet
Ivanti
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Français Patch Tuesday - Juillet
Ivanti
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 

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