SlideShare a Scribd company logo
Core Java Training
Exception Handling
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 (15)

PPTX
Session 09 - OOP with Java - Part 3
PawanMM
 
PPSX
OOP with Java - Part 3
Hitesh-Java
 
PPTX
Session 04 - Arrays in Java
PawanMM
 
PPSX
Practice Session
Hitesh-Java
 
PPSX
Arrays in Java
Hitesh-Java
 
PPSX
Review Session and Attending Java Interviews
Hitesh-Java
 
PPTX
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
PPSX
Intro to Object Oriented Programming with Java
Hitesh-Java
 
PPTX
Session 05 - Strings in Java
PawanMM
 
PDF
JAVA PROGRAMMING- Exception handling - Multithreading
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
Java Reflection @KonaTechAdda
Md Imran Hasan Hira
 
PPTX
Java reflection
NexThoughts Technologies
 
PPTX
Object+oriented+programming+in+java
Ye Win
 
PPT
core java
Vinodh Kumar
 
PPTX
Basics of Java
Sherihan Anver
 
Session 09 - OOP with Java - Part 3
PawanMM
 
OOP with Java - Part 3
Hitesh-Java
 
Session 04 - Arrays in Java
PawanMM
 
Practice Session
Hitesh-Java
 
Arrays in Java
Hitesh-Java
 
Review Session and Attending Java Interviews
Hitesh-Java
 
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
Intro to Object Oriented Programming with Java
Hitesh-Java
 
Session 05 - Strings in Java
PawanMM
 
JAVA PROGRAMMING- Exception handling - Multithreading
Jyothishmathi Institute of Technology and Science Karimnagar
 
Java Reflection @KonaTechAdda
Md Imran Hasan Hira
 
Java reflection
NexThoughts Technologies
 
Object+oriented+programming+in+java
Ye Win
 
core java
Vinodh Kumar
 
Basics of Java
Sherihan Anver
 

Similar to Exception Handling - Continued (20)

PPTX
Exception Handling
RatnaJava
 
PPTX
Session 13 - Exception handling - continued
PawanMM
 
PPTX
Session 12 - Exception Handling - Part 1
PawanMM
 
PPTX
Exception handling in java
Lovely Professional University
 
PPTX
UNIT 2.pptx
EduclentMegasoftel
 
PPT
Exceptions
Soham Sengupta
 
DOCX
What is an exception in java?
Pramod Yadav
 
PPTX
Exception handling in java.pptx
Nagaraju Pamarthi
 
PPTX
12. Java Exceptions and error handling
Intro C# Book
 
PPTX
Java - Exception Handling
Prabhdeep Singh
 
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PPT
Exception handling
Tata Consultancy Services
 
PPTX
12. Exception Handling
Intro C# Book
 
PPTX
Exception handling
Ardhendu Nandi
 
PPT
Exception
Марія Русин
 
PPTX
Exception handling in Java
Prasad Sawant
 
PPTX
Satish training ppt
satish lariya
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
Exception Handling
RatnaJava
 
Session 13 - Exception handling - continued
PawanMM
 
Session 12 - Exception Handling - Part 1
PawanMM
 
Exception handling in java
Lovely Professional University
 
UNIT 2.pptx
EduclentMegasoftel
 
Exceptions
Soham Sengupta
 
What is an exception in java?
Pramod Yadav
 
Exception handling in java.pptx
Nagaraju Pamarthi
 
12. Java Exceptions and error handling
Intro C# Book
 
Java - Exception Handling
Prabhdeep Singh
 
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Exception handling
Tata Consultancy Services
 
12. Exception Handling
Intro C# Book
 
Exception handling
Ardhendu Nandi
 
Exception handling in Java
Prasad Sawant
 
Satish training ppt
satish lariya
 
Java exception handling
BHUVIJAYAVELU
 
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
Collections - Lists, Sets
Hitesh-Java
 
PPSX
Collections - Sorting, Comparing Basics
Hitesh-Java
 
PPSX
Collections - Array List
Hitesh-Java
 
PPSX
Object Class
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
 
Collections - Lists, Sets
Hitesh-Java
 
Collections - Sorting, Comparing Basics
Hitesh-Java
 
Collections - Array List
Hitesh-Java
 
Object Class
Hitesh-Java
 
Ad

Recently uploaded (20)

PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 

Exception Handling - Continued

  • 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()