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

What's hot (15)

Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3
 
OOP with Java - Part 3
OOP with Java - Part 3OOP with Java - Part 3
OOP with Java - Part 3
 
Session 04 - Arrays in Java
Session 04 - Arrays in JavaSession 04 - Arrays in Java
Session 04 - Arrays in Java
 
Practice Session
Practice Session Practice Session
Practice Session
 
Arrays in Java
Arrays in Java Arrays in Java
Arrays in Java
 
Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Review Session and Attending Java Interviews
Review Session and Attending Java Interviews
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
 
Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java
 
Session 05 - Strings in Java
Session 05 - Strings in JavaSession 05 - Strings in Java
Session 05 - Strings in Java
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
 
Java Reflection @KonaTechAdda
Java Reflection @KonaTechAddaJava Reflection @KonaTechAdda
Java Reflection @KonaTechAdda
 
Java reflection
Java reflectionJava reflection
Java reflection
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
core java
core javacore java
core java
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 

Similar to Exception Handling - Continued

Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Prasad Sawant
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 

Similar to Exception Handling - Continued (20)

Exception Handling
Exception Handling  Exception Handling
Exception Handling
 
Session 13 - Exception handling - continued
Session 13 - Exception handling - continuedSession 13 - Exception handling - continued
Session 13 - Exception handling - continued
 
Session 12 - Exception Handling - Part 1
Session 12 - Exception Handling - Part 1Session 12 - Exception Handling - Part 1
Session 12 - Exception Handling - Part 1
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Exeption handling
Exeption handlingExeption handling
Exeption handling
 
Exceptions
ExceptionsExceptions
Exceptions
 
What is an exception in java?
What is an exception in java?What is an exception in java?
What is an exception in java?
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception
ExceptionException
Exception
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Satish training ppt
Satish training pptSatish training ppt
Satish training ppt
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 

More from Hitesh-Java

More from Hitesh-Java (20)

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

Recently uploaded

Recently uploaded (20)

Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 

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