Answered step by step
Verified Expert Solution
Link Copied!

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 definition

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 m1() 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.println("Top of m1"); try { if (t > 10) throw new MyCheckedException("t is large"); } catch (MyCheckedException e) { System.out.println("Error caught"); } System.out.println("Bottom of m1"); } 9. Having a try-catch statement like this removes the compiler-error and does not cause any new compiler-errors in the file. Run the file. You can see that it compiled and ran properly. 10. Change the 5's to 15's in the two method calls in main (i.e. m1(15) and m2(15)) and run it again. 11. Notice that in m1(), there are 3 lines print out: "Top of m1", "Error caught", and "Bottom of m1". But in m2(), there is only 1 line printed out before the exception text (in red font): "Top of m2". Why does "Bottom of m2" not get printed out? Write your answer in AnswersLab6.txt.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions