Given code:
//Class definition
public class ChainedExcptionDemo
{
//Main method
public static void main (String[ ] args)
{
//Try block
try {
//Call method() method
method1() ;
}
//Catch block
catch (Exception ex) {
// printStackTrace() method displays the
//exception name and description and also //print line name of error
ex.printStackTrace();
}
}
//Method definition
public static void method1() throws Exception
{
//In try block
try {
//Call method() method
method2() ;
}
//Catch block
catch (Exception ex) {
//Line16 statement to replace
//Throw the checked exception
throw new Exception ("New info from method1", ex);
}
}
//Method definition
public static void method2( ) throws Exception
{
//Throw the checked exception
throw new Exception( "New info from method2" );
}
}
Want to see the full answer?
Check out a sample textbook solutionChapter 12 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version, Student Value Edition (11th Edition)
- Write a class TimeOfDay that uses the exception classes defined in the previous exercise. Give it a method setTimeTo(timeString) that changes the time if timeString corresponds to a valid time of day. If not, it should throw an exception of the appropriate type. [Java]arrow_forwardWhat is the difference between using @Test(expected = TypeOfException) and using try/catch when testing if a method or constructor properly returns an exception? A. They do the same thing. B. @Test(expected = ...) is the proper way to unit test in java, and try/catch should only be used when unit testing in Python. C. @Test(expected = ...) will give a passing test as soon as one exception of the proper type is thrown and then stop the test, but using try/catch will allow you to test many examples in the same test and will only stop after running all examples or reaching a fail() or an assert statement that fails. D. We should never use try/catch when unit testing, otherwise we will catch the exception and then won't know if it is actually thrown.arrow_forwardDerive exception classes from the class you wrote in the previous exercise. Each new class should indicate a specific kind of error. For example, InvalidHourException could be used to indicate that the value entered for an hour was not an integer in the range of 1 to 12. (Also the previous answer was not preferd from the teacher cause its not simple and use def which we arent taken it)arrow_forward
- What is the value of the expression max().) O None It raises a ValueError exception. O It raises an IndexError exception.arrow_forwardIn Java when a method throws a checked Exception, what must we always do when calling that method? A. Make sure to declare the method as static. B. Put it inside a try/catch block. C. Set the permission level of the method to private. D. Check our input, and if it will be bad input we must throw an exception before calling the method.arrow_forwardPlease answer question. This is pertaining to Java programming language 3-19arrow_forward
- 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.arrow_forwardWhen testing exception cases with assertThrows, what is the best practice? A. We need to check the state of the object after the assertThrows to ensure the post-conditions of the exception are as intended. B. Trick question! We do not test exception cases! C. We should assert that the assertThrows checks for the most general (or highest-level) exception possible D. All of our exception tests should be in one single test method.arrow_forward5. Write a JAVA program to define a static method checkPT() to receive 3 positive integer parameters and checks whether they form Pythagorean triplet. If yes, display appropriate message, otherwise, throw custom exception called InvalidPythagoreanTripletException. The checkPT() method does not handle the exception thrown. Write main method in the same class to read 3 integers and make call to checkPT() method infinitely. The InvalidPythagoreanTripletException exception object. Use throw, throws, try and catch keywords appropriately in the program. Also illustrate re-throwing an exception in the main method handles the exception by displaying the program.arrow_forward
- This is a java program question dont copy from any other source or internet for gods sake write the program and give me the output also write the code in your own imagination dont copy it from other source pleasearrow_forwardwrite a code in javaarrow_forwardJava : Write the code segments in main( ) and in methodA( ) for the following scenario: main( ) passes methodA an int variable called a. methodA generates an exception, but does not deal with it, if a is not between 1 and 100. Hint: main will try and catch the exception and methodA throws exception.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning