Question
Instructions Download the starting point code: Oops.java. Examine the code. Note: This is a very odd class. It is highly atypical to have Throwables as
Instructions
Download the starting point code: Oops.java. Examine the code.
Note: This is a very odd class. It is highly atypical to have Throwables as fields.
Please note: The changes in the following instruction are cumulative. Unless you are explicitly directed to do so, do not undo the previous change.
Compile and run the class as is. 1) What is the exception message you get? Include the stack trace in the answer.
I will give you this answer so that you know how to include the entire stack trace in your answers.
1) java.lang.RuntimeException: Instantiated in m4 at Oops.m4(Oops.java:27) at Oops.m3(Oops.java:22) at Oops.m2(Oops.java:18) at Oops.m1(Oops.java:12) at Oops.main(Oops.java:33)
Comment out the instantiation on line 27. Compile and run the class again. 2) What is the exception message you get now? Include the stack trace in the answer.
Comment out the instantiation on line 17. Compile and run the class again. 3) What is the exception message you get now? Include the stack trace in the answer.
Comment out the instantiation on line 8. Instantiate the exception within main. This will require a bit more work than adding a single line of code.* Do not make the field static at this time. Compile and run the class again. 4) What is the exception message you get now? Include the stack trace in the answer.
* The issue is that the field is not static, while main is. Notice that the method is also not static. To call the method, we use a reference to an Oops instance. You will need to change the code so that the reference can be used twice: once to bind a newly instantiated exception instance to the non-static field, x, and once to call the non-static method, m1.
Comment out the instantiation in main. Update the field declaration on line 4 to include an instantiation of the runtime exception. Compile and run the class again. 5) What is the exception message you get now? Include the stack trace in the answer.
Update the field declaration to be static. Keep the instantiation of the exception in the field declaration. Compile and run the class again. 6) What is the exception message you get now? Include the stack trace in the answer.
Examine the answers you have given for the first 6 questions. 7) Summarize what you have found. Comment on it both in terms of instantiating a Throwable and in terms of lifetime for objects.
Submission
Create an ASCII file with the answers to the highlighted questions.
Include your name and this lab number and name at the top of the file. Make sure you number your answers. Include at least one blank line after each of the answers.
Evaluation Criteria
To get a check:
- Reasonable answers for questions 1 - 6.
For "fun":
- A good answer for question 7.
public class Oops { // Instatatiate a RuntimeException here. RuntimeException x; public Oops() { // Instatatiate a RuntimeException here. x = new RuntimeException("Instantiated in the constructor"); } public void m1() { m2(); } public void m2() { // Instatatiate a RuntimeException here. x = new RuntimeException("Instantiated in m2"); m3(); } public void m3() { m4(); } public void m4() { // Instatatiate a RuntimeException here. x = new RuntimeException("Instantiated in m4"); throw x; } public static void main(String[] args) { // Instatatiate a RuntimeException here. new Oops().m1(); } }
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