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 (11th Edition)
- Derive 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_forwardWhat 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_forward
- Please answer question. This is pertaining to Java programming language 3-19arrow_forwardWhat 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_forward
- 5. 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_forwardThis 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_forward
- Java : 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_forwardModify your last GeoPoint program and add exception handling. Wrap the code inside the main “do another?” loop of your last program with a try block. Add three except blocks. The first will catch a TypeError exception and display “Wrong type of input!” The second will catch an exception of your choice and display a message that makes sense. The third will catch a generic Exception along with its object e and display the message “Something went wrong: “, e so that the error message e is displayed. The code will look something like this (yours may vary): doAnother = 'y' while doAnother == 'y': try …your code here… except TypeError : print "Wrong type of input!" except …exception you picked… : print …your custom message… except Exception,e: print "Something went wrong: ", e doAnother = raw_input('Do another (y/n)? ')arrow_forwardWrite a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning