0% found this document useful (0 votes)
3 views3 pages

7 Java Exception Propagation

The document explains Java Exception Propagation, detailing how exceptions are thrown from the top of the call stack and propagate downwards until caught. It provides examples demonstrating the handling of unchecked and checked exceptions, highlighting that unchecked exceptions are propagated while checked exceptions are not. The document includes code snippets to illustrate these concepts and their outputs.

Uploaded by

Anima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

7 Java Exception Propagation

The document explains Java Exception Propagation, detailing how exceptions are thrown from the top of the call stack and propagate downwards until caught. It provides examples demonstrating the handling of unchecked and checked exceptions, highlighting that unchecked exceptions are propagated while checked exceptions are not. The document includes code snippets to illustrate these concepts and their outputs.

Uploaded by

Anima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/14/2015 Java Exception Propagation - javatpoint

Content Menu ▼

It's gone. Undo

What was wrong with this ad?


Irrelevant Inappropriate Repetitive

Java Exception propagation It's gone. Undo


What was wrong
with this ad?
An exception is first thrown from the top of the stack and if it is not
Irrelevant
caught, it drops down the call stack to the previous method,If not
Inappropriate
caught there, the exception again drops down to the previous method, Repetitive
and so on until they are caught or until they reach the very bottom of
the call stack.This is called exception propagation.

Rule: By default Unchecked Exceptions are forwarded in calling


chain (propagated).

Program of Exception Propagation

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

Test it Now

Output:exception handled
normal flow...

http://www.javatpoint.com/exception-propagation 1/3
9/14/2015 Java Exception Propagation - javatpoint

In the above example exception occurs in m() method where it is not


handled,so it is propagated to previous n() method where it is not
handled, again it is propagated to p() method where exception is handled.

Exception can be handled in any method in call stack either in main()


method,p() method,n() method or m() method.

Rule: By default, Checked Exceptions are not forwarded in calling


chain (propagated).

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");
. }
. }
http://www.javatpoint.com/exception-propagation 2/3
9/14/2015 Java Exception Propagation - javatpoint

Test it Now

Output:Compile Time Error

← prev next →

http://www.javatpoint.com/exception-propagation 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy