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 Plus MyProgrammingLab with Pearson EText -- Access Card Package
- 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_forwardFoundation in java Please provide a basic exception code. Do not use input, output. Please provide code and explain. Thank you. Write the Java code for the following: Write the InvalidGradeException class that will be used below. Write the Java program that reads an integer grade from a user and checks that it is valid (between 0 and 100). If it is invalid an InvalidGradeException will be thrown, stating what the invalid grade is. This will be caught in the main( ) method which will write a message about the invalid grade. If the grade is valid, it is written to the monitor..arrow_forward
- 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_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_forward
- I am getting this error with the EXCEPTIONarrow_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_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
- When 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_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_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
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education