SlideShare a Scribd company logo
1 of 20
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

Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
Tech_MX
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,..."Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
Vladimir Ivanov
 

What's hot (20)

Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
GDG Madrid - Dart Event - By Iván Zaera
GDG Madrid - Dart Event - By Iván ZaeraGDG Madrid - Dart Event - By Iván Zaera
GDG Madrid - Dart Event - By Iván Zaera
 
Chap02
Chap02Chap02
Chap02
 
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,..."Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
"Formal Verification in Java" by Shura Iline, Vladimir Ivanov @ JEEConf 2013,...
 
Android course session 3 ( OOP ) part 1
Android course session 3 ( OOP ) part 1Android course session 3 ( OOP ) part 1
Android course session 3 ( OOP ) part 1
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
Php Inspections (EA Extended): if-conditions optimization
Php Inspections (EA Extended): if-conditions optimizationPhp Inspections (EA Extended): if-conditions optimization
Php Inspections (EA Extended): if-conditions optimization
 
Objected-Oriented Programming with Java
Objected-Oriented Programming with JavaObjected-Oriented Programming with Java
Objected-Oriented Programming with Java
 
21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Inner class
Inner classInner class
Inner class
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Diving into Java Class Loader
Diving into Java Class LoaderDiving into Java Class Loader
Diving into Java Class Loader
 

Similar to Session 13 - Exception handling - continued

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
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 

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

Exception Handling - Continued
Exception Handling - Continued Exception Handling - Continued
Exception Handling - Continued
 
Exception Handling
Exception Handling  Exception Handling
Exception Handling
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1
 
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
 
Exception
ExceptionException
Exception
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
 
Exceptions
ExceptionsExceptions
Exceptions
 
Exeption handling
Exeption handlingExeption handling
Exeption handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Exception Handling.pdf
Exception Handling.pdfException Handling.pdf
Exception Handling.pdf
 
Satish training ppt
Satish training pptSatish training ppt
Satish training ppt
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.pptx
 
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
 

More from PawanMM

More from PawanMM (20)

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

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

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