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)
- What is the keyword throw used for? What is the keyword throws used for? Can you throw multiple exceptions in one throw statement? Explain with at least 3 or more examples in javaarrow_forward*This is a handout from my class that I'm having trouble with I tried YouTube videos and I'm still lost please explain the answers as simply as you can and thank you.* A. Look over the following code fragment, give the output: try { method(); System.out.println("After the method call"); } catch (Exception ex) { System.out.println("Exception in main"); } catch (RuntimeException ex) { System.out.println("RuntimeException in main"); } static void method() throws Exception { System.out.println(1 / 0); } B. Write the code to load an array. Use simple I/O (system.out.print, etc.) to get integer values from the user and load the array (loop?). Your code has the potential to generate 2 exceptions: 1) InputMismatch (research this) and 2) IllegalValueException (assume this exists). If the 1st occurs, change the input to be 99, warn the user and continue. The 2nd exception will occur when the user enters a 0; if this occurs, notify the user that a 0 has been entered, and change the input to…arrow_forwardAssume you are considering writing a method and are deciding what should happen when given input that is outside of perfect input. Which of the following is not a way to handle this? Group of answer choices 1. Use better JUnit testing 2. Throw an exception 3. Try to reasonably auto-correct 4. Prevent the errorarrow_forward
- What is the output of the following code? a. Error: ArithmeticException b. No output c. Error: ArrayIndexOutOfBoundsException d. Warning: Some Other exception e. Error:ArithmeticException and Warning:Some Other exceptionarrow_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_forwardQ# When JUnit testing with exceptions, which of the following is true? Group of answer choices A successful test happens whenever an exception is not thrown. Exceptions and tests should not be used together. A failed test results when you catch an exception on valid input. If you give an illegal argument to a method the JUnit test will fail. Q# When you want a method to return nothing, what do you put for the return type? Group of answer choices boolean false void null Q# Which of the following statements are true? Group of answer choices All of the options are true. Methods can take multiple primitives as arguments. Methods can take objects of other classes as arguments. A method for a class can take an object of that class as an argument. Q# What does Java guarantee is part of every class? Group of answer choices AtoString() A set of Getter methods to retrieve class variables. A method to get the binary representation of the class. A set of private class…arrow_forward
- your code does not run Exception Handling correctly like shown in my 2nd picture . Can you please fix it ?arrow_forwardWhich of the following is a good example of the definition of a custom exception type?arrow_forwardWrite a method that will take in a time in 24 hour format (1430) and return the time in 12 hour format (2:30pm). This method should have appropriate exception handling. If an incorrect time is sent as an argument (something out of bounds or 23(#4k in other words garbage), then your method should throw a TimeFormatException which you define. In no case should your method throw an unhandled exception! Show how to test the method to be sure.arrow_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_forwardIf we don't know exception type, what type should we mention in catch block ?arrow_forwardWrite a program that converts a time from 24-hour notation to 12-hour notation. To make the solution easier, a requirement is imposed on the input: It must be in xx:xx format, i.e. it must have two digits, a colon, and then another two digits. Define an exception class called TimeException. If the user enters an illegal time, like 10:65, or even gibberish, like 8&*68, your program should throw and handle a TimeException. Test your program with the file "times.txt" as input and store the result in the file "result.txt" File times.txt: 00:00 12:00 12:01 11:59 23:59 24:00 10:65 3:23 1145 8&*68 File result.txt: # 24-hour 12-hour -------------------------------------------- 1 00:00 12:00 AM 2 12:00 12:00 PM 3 12:01 12:01 PM 4 11:59 11:59 AM 5 23:59 11:59 PM 6 24:00 Time Exception 7 10:65…arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning