Question

In: Computer Science

What is exception propagation? Give an example of a class that contains at least two methods,...

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.

Solutions

Expert Solution

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.

class TestExceptionPropagation{  

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

  }  

}  

Explanation:

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.


Related Solutions

What is vegetative propagation? Give two suitable examples.
What is vegetative propagation? Give two suitable examples with details.
1. Describe the difference between instance methods and class methods in Java and give an example...
1. Describe the difference between instance methods and class methods in Java and give an example of each. 2. A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application? 3. Describe the difference between abstract classes and concrete classes, giving an example of each. 4. Explain how data are encapsulated and information is hidden in Java? 5. Explain the difference between a class and an interface...
There are two types of propagation delay, identify and give their descriptions.
There are two types of propagation delay, identify and give their descriptions.
Write a Calculate class which contains methods that accepts two integers as arguments and return the...
Write a Calculate class which contains methods that accepts two integers as arguments and return the results of Addition, Subtraction, Division and Multiplication. Call these methods though main()and print the results.
Linear Algebra: Explain what a vector space is and offer an example that contains at least...
Linear Algebra: Explain what a vector space is and offer an example that contains at least five (5) of the ten (10) axioms for vector spaces.
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do...
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do the work
Give at Least two example of application of indifference curve technique in Context managerial Economic.
Give at Least two example of application of indifference curve technique in Context managerial Economic.
Give an example of an inner and outer class. (Java)
Give an example of an inner and outer class. (Java)
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
What is a two sampled independent test? Give an example.
What is a two sampled independent test? Give an example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT