What is exception propagation? Give an example of a class that contains at least
ID: 3682302 • Letter: W
Question
What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.
I'd really appreciate it if you don't just copy out of a book and paste here. Please explain it like you're teaching me. I am a little unclear on this stuff.
Explanation / Answer
An Exception Propagation is the process which contains atleast two methods to progate the
type of exceptions occured.
In this type ,an exception is first thrown from the top of the stack and if it is not caught,
it drops down the call stack to the previous method, If not caught there,
the exception again drops down to the previous method, and so on until they are caught or until they
reach the very bottom of the call stack. This is called exception propagation.
//Example program on Exception Propagation
//class PropagationException contains two methods display() and show() methods
//PropagationException.java
class PropagationException
{
void display() throws InterruptedException
{
Thread.sleep(1000); //throws checked exception
System.out.println("sleep() method in Thread class throws Checked exception....");
}
void show()
{
try
{
display();
}
catch(Exception e)
{
System.out.println("To handle the exception");
}
}
public static void main(String args[])
{
PropagationException pe=new PropagationException();
pe.show();
System.out.println("show() method calls sub ordinate method display() in the Program.....");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.