SlideShare a Scribd company logo
Java/J2EE Programming 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
Thank You

More Related Content

PPTX
Session 12 - Exception Handling - Part 1
ODP
Exception handling in java
PPTX
Exception Handling in Java
PPTX
Exception handling in java
PPTX
Test Driven Development
PPTX
PDF
Exception handling
PPTX
Automation test
Session 12 - Exception Handling - Part 1
Exception handling in java
Exception Handling in Java
Exception handling in java
Test Driven Development
Exception handling
Automation test

Similar to Exception Handling (20)

PPSX
Exception Handling - Continued
PPTX
Session 13 - Exception handling - continued
PPSX
Exception Handling - Part 1
PPTX
UNIT 2.pptx
PPTX
Exception handling in java
PPTX
Exception handling
DOCX
What is an exception in java?
PPTX
12. Java Exceptions and error handling
PPT
Exceptions
PPT
Exception handling
PPTX
Satish training ppt
PPT
PPTX
12. Exception Handling
PPTX
Java exception handling
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
PPTX
Interface andexceptions
PDF
JAVA PROGRAMMING- Exception handling - Multithreading
PPT
Exception Handling
Exception Handling - Continued
Session 13 - Exception handling - continued
Exception Handling - Part 1
UNIT 2.pptx
Exception handling in java
Exception handling
What is an exception in java?
12. Java Exceptions and error handling
Exceptions
Exception handling
Satish training ppt
12. Exception Handling
Java exception handling
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
Interface andexceptions
JAVA PROGRAMMING- Exception handling - Multithreading
Exception Handling
Ad

More from RatnaJava (14)

PPTX
Review Session and Attending Java Interviews
PPTX
Collections - Lists & sets
PPTX
Collections - Sorting, Comparing Basics
PPTX
Collections Array list
PPTX
Object Class
PPTX
OOPs with Java - Packaging and Access Modifiers
PPTX
OOP with Java - Abstract Classes and Interfaces
PPTX
OOP with Java - Part 3
PPTX
OOP with Java - continued
PPTX
Object Oriented Programming
PPTX
Data Handling and Function
PPTX
Introduction to Java Part-3
PPTX
Introduction to Java Part-2
PPTX
Introduction to Java
Review Session and Attending Java Interviews
Collections - Lists & sets
Collections - Sorting, Comparing Basics
Collections Array list
Object Class
OOPs with Java - Packaging and Access Modifiers
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Part 3
OOP with Java - continued
Object Oriented Programming
Data Handling and Function
Introduction to Java Part-3
Introduction to Java Part-2
Introduction to Java
Ad

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
Teaching material agriculture food technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
The Rise and Fall of 3GPP – Time for a Sabbatical?
Mobile App Security Testing_ A Comprehensive Guide.pdf
cuic standard and advanced reporting.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectroscopy.pptx food analysis technology
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx
Teaching material agriculture food technology
Machine learning based COVID-19 study performance prediction
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
“AI and Expert System Decision Support & Business Intelligence Systems”
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation theory and applications.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton

Exception Handling

  • 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"); } } }