Question: Consider the following code snippet with exceptions. Note that the program execution starts with main: public class NestedTryCatch { static class CustomException extends Exception; public

Consider the following code snippet with exceptions. Note that the program execution starts
with main:
public class NestedTryCatch {
static class CustomException extends Exception;
public static void main(String [] args){
try {
try {
methodA();
System.out.println("D");
} catch (CustomException e){
System.out.println("Inner catch: "+ e.getMessage());
}
methodB();
System.out.println("E");
} catch (CustomException e){
}
System.out.println("Outer catch: "+ e.getMessage());
}
public static void methodA() throws CustomException {
try {
methodB();
System.out.println("C");
throw new CustomException("Exception from A");
} catch (CustomException e){
System.out.println("Catch in A: "+ e.getMessage());
throw e;
}
}
public static void methodB() throws CustomException {
throw new CustomException("Exception from B");
}
}
Write down what will be the output by the given program and briefly justify your
answer.
Modify your code to catch the exceptions thrown by both methodA and methodB,
with the output including but not limited to "Outer catch: Exception from A", i.e
your Outer catch should only handle the exception from methodA. Write the expected
output from your modified code and explain the changes you made.Problem 4[12pt] Consider the following code snippet with exceptions. Note that the main function calls foo in the nested try block. a)(4pt) Write down what will be printed out by the program and briefly justify your answer. b)(8 pt ) Instead of the "replacement" semantics of exception handling used in modern languages (i.e., the semantics introduced in lecture), a very early design of exception handling introduced in the PL/I language uses a "binding" semantics. In particular, the design dynamically tracks a sequence of "catch" blocks that are currently active: a catch block is active whenever the corresponding try block is active. Moreover, whenever an exception is thrown. the sequence of active "catch" blocks will be traversed (in a first-in-last-out manner) to find a matching handler. Furthermore, execution will resume at the statement following the one that throws exception, rather than the next statement after the matching "catch" block as we have seen in the "replacement" semantics.Write down what will be printed out by the program if the language uses the "binding" semanties, and briefly justify your answer.
 Consider the following code snippet with exceptions. Note that the program

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 Databases Questions!