SlideShare a Scribd company logo
Java & JEE Training
Day 12 – 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)

PPT
Exception handling
Tata Consultancy Services
 
PPTX
Exception handling in java
Lovely Professional University
 
PPTX
Exception handling in Java
Abhishek Pachisia
 
PPT
Java exception
Arati Gadgil
 
PPT
Types of exceptions
myrajendra
 
PPTX
Exception handling
Shashwat Shriparv
 
PPTX
Exception Handling in Java
Ganesh kumar reddy
 
PPT
exception handling in java
aptechsravan
 
PDF
Built in exceptions
TharuniDiddekunta
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPTX
Exception handling in java
pooja kumari
 
PPTX
Exceptions in java
Sameer Patil
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PPT
Exception handling
M Vishnuvardhan Reddy
 
PPTX
Exception handling in java
ARAFAT ISLAM
 
PPTX
Chap2 exception handling
raksharao
 
PPTX
Exception Handling in Java
lalithambiga kamaraj
 
PDF
javaexceptions
Arjun Shanka
 
PPTX
Exception handling in java
yugandhar vadlamudi
 
Exception handling
Tata Consultancy Services
 
Exception handling in java
Lovely Professional University
 
Exception handling in Java
Abhishek Pachisia
 
Java exception
Arati Gadgil
 
Types of exceptions
myrajendra
 
Exception handling
Shashwat Shriparv
 
Exception Handling in Java
Ganesh kumar reddy
 
exception handling in java
aptechsravan
 
Built in exceptions
TharuniDiddekunta
 
Java exception handling
BHUVIJAYAVELU
 
Exception handling in java
pooja kumari
 
Exceptions in java
Sameer Patil
 
Exception Handling in JAVA
SURIT DATTA
 
Exception handling
M Vishnuvardhan Reddy
 
Exception handling in java
ARAFAT ISLAM
 
Chap2 exception handling
raksharao
 
Exception Handling in Java
lalithambiga kamaraj
 
javaexceptions
Arjun Shanka
 
Exception handling in java
yugandhar vadlamudi
 

Similar to Session 12 - Exception Handling - Part 1 (20)

PPSX
Exception Handling - Part 1
Hitesh-Java
 
DOCX
Exception handling in java
gopalrajput11
 
PPTX
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
PPTX
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
PPTX
java exception.pptx
SukhpreetSingh519414
 
PDF
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
PPT
Exceptions in java
JasmeetSingh326
 
PPTX
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
PPTX
Exception handling in java
pooja kumari
 
PPTX
L14 exception handling
teach4uin
 
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PPTX
PACKAGES, INTERFACES AND EXCEPTION HANDLING
mohanrajm63
 
PPTX
Java -Exception handlingunit-iv
RubaNagarajan
 
DOCX
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
DOCX
Unit5 java
mrecedu
 
PDF
Java exception handling ppt
JavabynataraJ
 
PPT
Computer Object Oriented Programming - Chapter 4 - Excption Handling.ppt
ShafiEsa1
 
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
PPTX
Java exception handling
Md. Tanvir Hossain
 
Exception Handling - Part 1
Hitesh-Java
 
Exception handling in java
gopalrajput11
 
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
java exception.pptx
SukhpreetSingh519414
 
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
Exceptions in java
JasmeetSingh326
 
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
Exception handling in java
pooja kumari
 
L14 exception handling
teach4uin
 
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PACKAGES, INTERFACES AND EXCEPTION HANDLING
mohanrajm63
 
Java -Exception handlingunit-iv
RubaNagarajan
 
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
Unit5 java
mrecedu
 
Java exception handling ppt
JavabynataraJ
 
Computer Object Oriented Programming - Chapter 4 - Excption Handling.ppt
ShafiEsa1
 
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
Java exception handling
Md. Tanvir Hossain
 
Ad

More from PawanMM (20)

PPTX
Session 48 - JS, JSON and AJAX
PawanMM
 
PPTX
Session 46 - Spring - Part 4 - Spring MVC
PawanMM
 
PPTX
Session 45 - Spring - Part 3 - AOP
PawanMM
 
PPTX
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PawanMM
 
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
PPTX
Session 42 - Struts 2 Hibernate Integration
PawanMM
 
PPTX
Session 41 - Struts 2 Introduction
PawanMM
 
PPTX
Session 40 - Hibernate - Part 2
PawanMM
 
PPTX
Session 39 - Hibernate - Part 1
PawanMM
 
PPTX
Session 38 - Core Java (New Features) - Part 1
PawanMM
 
PPTX
Session 37 - JSP - Part 2 (final)
PawanMM
 
PPTX
Session 36 - JSP - Part 1
PawanMM
 
PPTX
Session 35 - Design Patterns
PawanMM
 
PPTX
Session 34 - JDBC Best Practices, Introduction to Design Patterns
PawanMM
 
PPTX
Session 33 - Session Management using other Techniques
PawanMM
 
PPTX
Session 32 - Session Management using Cookies
PawanMM
 
PPTX
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
PawanMM
 
PPTX
Session 30 - Servlets - Part 6
PawanMM
 
PPTX
Session 29 - Servlets - Part 5
PawanMM
 
PPTX
Session 28 - Servlets - Part 4
PawanMM
 
Session 48 - JS, JSON and AJAX
PawanMM
 
Session 46 - Spring - Part 4 - Spring MVC
PawanMM
 
Session 45 - Spring - Part 3 - AOP
PawanMM
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PawanMM
 
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
Session 42 - Struts 2 Hibernate Integration
PawanMM
 
Session 41 - Struts 2 Introduction
PawanMM
 
Session 40 - Hibernate - Part 2
PawanMM
 
Session 39 - Hibernate - Part 1
PawanMM
 
Session 38 - Core Java (New Features) - Part 1
PawanMM
 
Session 37 - JSP - Part 2 (final)
PawanMM
 
Session 36 - JSP - Part 1
PawanMM
 
Session 35 - Design Patterns
PawanMM
 
Session 34 - JDBC Best Practices, Introduction to Design Patterns
PawanMM
 
Session 33 - Session Management using other Techniques
PawanMM
 
Session 32 - Session Management using Cookies
PawanMM
 
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
PawanMM
 
Session 30 - Servlets - Part 6
PawanMM
 
Session 29 - Servlets - Part 5
PawanMM
 
Session 28 - Servlets - Part 4
PawanMM
 
Ad

Recently uploaded (20)

PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Practical Applications of AI in Local Government
OnBoard
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Kubernetes - Architecture & Components.pdf
geethak285
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 

Session 12 - Exception Handling - Part 1

  • 1. Java & JEE Training Day 12 – Exception Handling
  • 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