Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise 2 - Checked and Unchecked Exceptions 1. Create a new class and call it MyCheckedException. Once the class has been created, modify the class
Exercise 2 - Checked and Unchecked Exceptions 1. Create a new class and call it MyCheckedException. Once the class has been created, modify the class definition to include "extends Exception" after the class name so that it inherits from the Exception class. 2. In this class, create a constructor which takes in a String parameter. Inside the constructor, you just need one line: super(str) where str is the String parameter. 3. Repeat these last 2 steps but this time call the new class MyUncheckedException and make it extend (inherit from) RuntimeException instead of Exception. 4. Open TestMyExceptions.java and examine the code. What is the difference between the methods m1() and m2() ? Why does one show a compiler error but not the other? Write this information in AnswersLab6.txt. 5. Click on the little red X indicating a compiler error on Line 7 . What is/are the suggested fix(es)? Write this information in AnswersLab6.txt. Do not click the suggested fixes. 6. Examine the method calls to m1() and m2() from the main method. Based on the values sent in for the parameter t into these methods, would the exceptions be thrown when executed (assuming it would compile as is). Write your answer in AnswersLab6.txt 7. Now add the text throws MyCheckedException in line 4 after the m1() method signature (just before the opening curly bracket). Notice this resolves the original compiler error but now there is a compiler error on the method call to m1( ) from main(). Why do you think the compiler error is now showing up in the method call to m1() ? Write your answer in AnswersLab6.txt. 8. Remove the text throws MyCheckedException that you just added. Now add a try-block around the if-statement in m11 () with a catch clause that catches MyCheckedException. m1 () should now look like this (the new parts are shown in bold text): public static void m1 (int t ) \{ system.out.print7n("Top of m1; try \{ if (t>10)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started