Question: intro to java ( exceptions chapter) What output is produced by the following code? int waitTime = 46; try { System.out.println(Try blockentered.); if (waitTime> 30)

intro to java ( exceptions chapter)

What output is produced by the following code?

int waitTime = 46;
try
{
System.out.println("Try blockentered.");
if (waitTime> 30)
throw newException("Time Limit Exceeded.");
System.out.println("Leaving tryblock.");
}
catch(Exception e)
{
System.out.println("Exception: " +e.getMessage());
}
System.out.println("After catch block");

2. What output would the code in the previous questionproduce if we changed 46 in the first statement to 12?

3. What is an exception? Is it an identifier? A variable? Amethod? An object? A class? Something else?

4. Is the following statement legal?

Exception myException = new Exception("HiMom!");

5. In the example code in lesson, howwould the program’s behavior change if you were to replace

if (milkCount < 1)
throw new Exception("Exception: Nomilk!");

with the following?

if (milkCount < 1)
{ Exception e = newException("Exception: No milk!");

throw e;
}

6. What happens when a throw statement is executed?

7. In the code given in Question 1, identify the tryblock.

8. In the code given in Question 1, identify the catchblock.

9. In the code given in Question 1, identify thecatch-block parameter.

10. Would the code given in Question 1 perform anydifferently if the catch block were changed to thefollowing?

catch(Exception messenger)
{
System.out.println("Exception: "+
messenger.getMessage());
}

11. Write a statement that will throw an exception of typeException if the value of the String variable named status is"bad". The string recovered by getMessage should be "Exceptionthrown: Bad Status." You need not write the try block and catchblock.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!