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

Java Question: What is exception propagation? Give an example of a class that contains at least...
Java 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.
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.
Name and give examples of at least two (2) different methods to manage risk.
Name and give examples of at least two (2) different methods to manage risk.
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
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods:...
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods: /** Convert from Celsius to Fahrenheit */ public static double celsiusToFahrenheit(double celsius) /** Convert from Fahrenheit to Celsius */ public static double fahrenheitToCelsius(double fahrenheit) The formula for the conversion is: Celsius = (5.0 / 9) * (Fahrenheit – 32) Fahrenheit = (9.0 / 5) * Celsius + 32 Program output should be like: If you want to convert from Fahrenheit To Celsius press 0...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT