SlideShare a Scribd company logo
Java & JEE Training
Day 13 – Exception Handling (Contd.)
Page 1Classification: Restricted
Agenda
• throw and throws keywords
• Exception propagation – the cases of Checked and Unchecked Exceptions
• Defining your own custom Exception
Page 2Classification: Restricted
Review of Exception handling concepts from last session…
• What is an exception? What is an error?
• Throwable class – parent of Exception and Error
• Types of Exception: Checked and Unchecked Exceptions
Page 3Classification: Restricted
Checked vs Unchecked Exceptions – Hierarchy review
• Checked Exceptions = Subclass of Exception
• Unchecked Exceptions = Subclass of RuntimeException
Page 4Classification: Restricted
Throwing exception… “throw” keyword
public class TestThrow1{
static void validate(int age){
if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[]){
validate(13);
System.out.println("rest of the code...");
}
}
Page 5Classification: Restricted
Exception propagation example.. Unchecked exceptions are
propagated through the method call stack
class TestExceptionPropagation1{
void m(){
int data=50/0;
}
void n(){
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handled");}
}
public static void main(String args[]){
TestExceptionPropagation1 obj=new TestExceptionPropagation1();
obj.p();
System.out.println("normal flow...");
}
}
Page 6Classification: Restricted
Program which describes that checked
exceptions are not propagated
class TestExceptionPropagation2{
void m(){
throw new java.io.IOException("device error");//checked exception
}
void n(){
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handeled");}
}
public static void main(String args[]){
TestExceptionPropagation2 obj=new TestExceptionPropagation2();
obj.p();
System.out.println("normal flow");
}
}
Page 7Classification: Restricted
Java throws keyword
The Java throws keyword is used to declare an exception. It gives an
information to the programmer that there may occur an exception so it is
better for the programmer to provide the exception handling code so that
normal flow can be maintained.
return_type method_name() throws exception_class_name{
//method code
}
Only checked exceptions should be declared, because
• unchecked Exception: under your control so correct your code.
• error: beyond your control e.g. you are unable to do anything if there
occurs VirtualMachineError or StackOverflowError.
Page 8Classification: Restricted
Java throws example
import java.io.IOException;
class Testthrows1{
void m()throws IOException{
throw new IOException("device error");//checked exception
}
void n()throws IOException{
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handled");}
}
public static void main(String args[]){
Testthrows1 obj=new Testthrows1();
obj.p();
System.out.println("normal flow...");
}
}
Page 9Classification: Restricted
Difference between throw and throws in Java
Page 10Classification: Restricted
Difference between throw and throws in Java
void m(){
throw new ArithmeticException("sorry");
}
void m()throws ArithmeticException{
//method code
}
void m()throws ArithmeticException{
throw new ArithmeticException("sorry");
}
Page 11Classification: Restricted
Unchecked Exceptions? Why?
http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
Please read the article and let’s discuss this in next class.
Page 12Classification: Restricted
Difference between final, finally and finalize
Page 13Classification: Restricted
What is the output?
public class Test
{
public static void aMethod() throws Exception
{
try /* Line 5 */
{
throw new Exception(); /* Line 7 */
}
finally /* Line 9 */
{
System.out.print("finally "); /* Line 11 */
}
}
public static void main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /* Line 20 */
{
System.out.print("exception ");
}
System.out.print("finished"); /* Line 24 */
}
}
Page 14Classification: Restricted
What is the output?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {}
}
Page 15Classification: Restricted
What is the output?
public class X
{
public static void main(String [] args)
{
try
{
badMethod(); /* Line 7 */
System.out.print("A");
}
catch (Exception ex) /* Line 10 */
{
System.out.print("B"); /* Line 12 */
}
finally /* Line 14 */
{
System.out.print("C"); /* Line 16 */
}
System.out.print("D"); /* Line 18 */
}
public static void badMethod()
{
throw new RuntimeException();
}
}
Page 16Classification: Restricted
What is the output?
public class MyProgram
{
public static void main(String args[])
{
try
{
System.out.print("Hello world ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
Page 17Classification: Restricted
What is the output?
class Exc0 extends Exception { }
class Exc1 extends Exc0 { } /* Line 2 */
public class Test
{
public static void main(String args[])
{
try
{
throw new Exc1(); /* Line 9 */
}
catch (Exc0 e0) /* Line 11 */
{
System.out.println("Ex0 caught");
}
catch (Exception e)
{
System.out.println("exception caught");
}
}
}
Page 18Classification: Restricted
Topics to be covered in next session
• Object Class
• toString()
• equals()
• Hashing
• hashCode()
Page 19Classification: Restricted
Thank you!

More Related Content

What's hot (20)

PPTX
Inner Classes & Multi Threading in JAVA
Tech_MX
 
PPT
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
PDF
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
ODP
GDG Madrid - Dart Event - By Iván Zaera
Jc Miñarro
 
PPT
Chap02
Jotham Gadot
 
PDF
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
Vladimir Ivanov
 
PDF
Android course session 3 ( OOP ) part 1
Keroles M.Yakoub
 
PPTX
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
PDF
Php Inspections (EA Extended): if-conditions optimization
Vladimir Reznichenko
 
PPT
Objected-Oriented Programming with Java
Oum Saokosal
 
PDF
21 Essential JavaScript Interview Questions
Arc & Codementor
 
PDF
Java Inner Classes
Jussi Pohjolainen
 
PPTX
Inner class
Guna Sekaran
 
PPT
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
Mahmoud Alfarra
 
PPTX
Chapter2 array of objects
Mahmoud Alfarra
 
PPT
L5 classes, objects, nested and inner class
teach4uin
 
PPTX
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
PPTX
Java- Nested Classes
Prabhdeep Singh
 
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPTX
Diving into Java Class Loader
Md Imran Hasan Hira
 
Inner Classes & Multi Threading in JAVA
Tech_MX
 
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
GDG Madrid - Dart Event - By Iván Zaera
Jc Miñarro
 
Chap02
Jotham Gadot
 
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
Vladimir Ivanov
 
Android course session 3 ( OOP ) part 1
Keroles M.Yakoub
 
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
Php Inspections (EA Extended): if-conditions optimization
Vladimir Reznichenko
 
Objected-Oriented Programming with Java
Oum Saokosal
 
21 Essential JavaScript Interview Questions
Arc & Codementor
 
Java Inner Classes
Jussi Pohjolainen
 
Inner class
Guna Sekaran
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
Mahmoud Alfarra
 
Chapter2 array of objects
Mahmoud Alfarra
 
L5 classes, objects, nested and inner class
teach4uin
 
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
Java- Nested Classes
Prabhdeep Singh
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Diving into Java Class Loader
Md Imran Hasan Hira
 

Similar to Session 13 - Exception handling - continued (20)

PPTX
Exception Handling
RatnaJava
 
PPSX
Exception Handling - Continued
Hitesh-Java
 
PPTX
Session 12 - Exception Handling - Part 1
PawanMM
 
PPTX
Exception handling in java
pooja kumari
 
PPTX
Exception handling in java
pooja kumari
 
PPTX
Exceptionhandling
Nuha Noor
 
PPSX
Exception Handling - Part 1
Hitesh-Java
 
PPTX
Exception handling
Shashwat Shriparv
 
PPT
Exception handling
Abishek Purushothaman
 
PPTX
Exception handling in java-PPT.pptx
sonalipatil225940
 
PPTX
java exception.pptx
SukhpreetSingh519414
 
PDF
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
PPTX
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
PPTX
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
PPT
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
ssuserf45a65
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPSX
Java Exceptions
jalinder123
 
PPSX
Java Exceptions Handling
DrRajeshreeKhande
 
PPTX
Nalinee java
Nalinee Choudhary
 
PPTX
Exception handling in java.pptx
Nagaraju Pamarthi
 
Exception Handling
RatnaJava
 
Exception Handling - Continued
Hitesh-Java
 
Session 12 - Exception Handling - Part 1
PawanMM
 
Exception handling in java
pooja kumari
 
Exception handling in java
pooja kumari
 
Exceptionhandling
Nuha Noor
 
Exception Handling - Part 1
Hitesh-Java
 
Exception handling
Shashwat Shriparv
 
Exception handling
Abishek Purushothaman
 
Exception handling in java-PPT.pptx
sonalipatil225940
 
java exception.pptx
SukhpreetSingh519414
 
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
ssuserf45a65
 
Java exception handling
BHUVIJAYAVELU
 
Java Exceptions
jalinder123
 
Java Exceptions Handling
DrRajeshreeKhande
 
Nalinee java
Nalinee Choudhary
 
Exception handling in java.pptx
Nagaraju Pamarthi
 
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
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 

Session 13 - Exception handling - continued

  • 1. Java & JEE Training Day 13 – Exception Handling (Contd.)
  • 2. Page 1Classification: Restricted Agenda • throw and throws keywords • Exception propagation – the cases of Checked and Unchecked Exceptions • Defining your own custom Exception
  • 3. Page 2Classification: Restricted Review of Exception handling concepts from last session… • What is an exception? What is an error? • Throwable class – parent of Exception and Error • Types of Exception: Checked and Unchecked Exceptions
  • 4. Page 3Classification: Restricted Checked vs Unchecked Exceptions – Hierarchy review • Checked Exceptions = Subclass of Exception • Unchecked Exceptions = Subclass of RuntimeException
  • 5. Page 4Classification: Restricted Throwing exception… “throw” keyword public class TestThrow1{ static void validate(int age){ if(age<18) throw new ArithmeticException("not valid"); else System.out.println("welcome to vote"); } public static void main(String args[]){ validate(13); System.out.println("rest of the code..."); } }
  • 6. Page 5Classification: Restricted Exception propagation example.. Unchecked exceptions are propagated through the method call stack class TestExceptionPropagation1{ void m(){ int data=50/0; } void n(){ m(); } void p(){ try{ n(); }catch(Exception e){System.out.println("exception handled");} } public static void main(String args[]){ TestExceptionPropagation1 obj=new TestExceptionPropagation1(); obj.p(); System.out.println("normal flow..."); } }
  • 7. Page 6Classification: Restricted Program which describes that checked exceptions are not propagated class TestExceptionPropagation2{ void m(){ throw new java.io.IOException("device error");//checked exception } void n(){ m(); } void p(){ try{ n(); }catch(Exception e){System.out.println("exception handeled");} } public static void main(String args[]){ TestExceptionPropagation2 obj=new TestExceptionPropagation2(); obj.p(); System.out.println("normal flow"); } }
  • 8. Page 7Classification: Restricted Java throws keyword The Java throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained. return_type method_name() throws exception_class_name{ //method code } Only checked exceptions should be declared, because • unchecked Exception: under your control so correct your code. • error: beyond your control e.g. you are unable to do anything if there occurs VirtualMachineError or StackOverflowError.
  • 9. Page 8Classification: Restricted Java throws example import java.io.IOException; class Testthrows1{ void m()throws IOException{ throw new IOException("device error");//checked exception } void n()throws IOException{ m(); } void p(){ try{ n(); }catch(Exception e){System.out.println("exception handled");} } public static void main(String args[]){ Testthrows1 obj=new Testthrows1(); obj.p(); System.out.println("normal flow..."); } }
  • 10. Page 9Classification: Restricted Difference between throw and throws in Java
  • 11. Page 10Classification: Restricted Difference between throw and throws in Java void m(){ throw new ArithmeticException("sorry"); } void m()throws ArithmeticException{ //method code } void m()throws ArithmeticException{ throw new ArithmeticException("sorry"); }
  • 12. Page 11Classification: Restricted Unchecked Exceptions? Why? http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html Please read the article and let’s discuss this in next class.
  • 13. Page 12Classification: Restricted Difference between final, finally and finalize
  • 14. Page 13Classification: Restricted What is the output? public class Test { public static void aMethod() throws Exception { try /* Line 5 */ { throw new Exception(); /* Line 7 */ } finally /* Line 9 */ { System.out.print("finally "); /* Line 11 */ } } public static void main(String args[]) { try { aMethod(); } catch (Exception e) /* Line 20 */ { System.out.print("exception "); } System.out.print("finished"); /* Line 24 */ } }
  • 15. Page 14Classification: Restricted What is the output? public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (Exception ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } public static void badMethod() {} }
  • 16. Page 15Classification: Restricted What is the output? public class X { public static void main(String [] args) { try { badMethod(); /* Line 7 */ System.out.print("A"); } catch (Exception ex) /* Line 10 */ { System.out.print("B"); /* Line 12 */ } finally /* Line 14 */ { System.out.print("C"); /* Line 16 */ } System.out.print("D"); /* Line 18 */ } public static void badMethod() { throw new RuntimeException(); } }
  • 17. Page 16Classification: Restricted What is the output? public class MyProgram { public static void main(String args[]) { try { System.out.print("Hello world "); } finally { System.out.println("Finally executing "); } } }
  • 18. Page 17Classification: Restricted What is the output? class Exc0 extends Exception { } class Exc1 extends Exc0 { } /* Line 2 */ public class Test { public static void main(String args[]) { try { throw new Exc1(); /* Line 9 */ } catch (Exc0 e0) /* Line 11 */ { System.out.println("Ex0 caught"); } catch (Exception e) { System.out.println("exception caught"); } } }
  • 19. Page 18Classification: Restricted Topics to be covered in next session • Object Class • toString() • equals() • Hashing • hashCode()