Suppose that statement2 causes an exception in the following

Suppose that statement2 causes an exception in the following try-catch block: (10 points)

public void m2() {

m1();

}

public void m1() {

try {

          statement1;

          statement2;

          statement3;

}

catch (Exception1 ex1) {

}

catch (Exception2 ex2) {

}

catch (Exception3 ex3) {

            throw ex3;

}

finally {

statement4;

}

Statement5;

}

Answer the following questions:

Will statement1 be executed?

Will statement3 be executed?

If the exception is not caught, will statement4be executed?

If the exception is not caught, will statement5be executed?

If the exception is caught in the catch block, will statement4 be executed?

If the exception is caught in the catch block, will statement5 be executed?

If the exception is of type Exception2, will statement4 be executed?

If the exception is of type Exception2, will statement5 be executed?

If the exception is of type Exception3, will statement4 be executed?

If the exception is of type Exception3, will statement5 be executed?

Solution

Will statement1 be executed?

            Answer : Yes, statement1 will be executed as this is the first statement and there was no exception yet.

Will statement3 be executed?

            Answer : Since there is an exception at statement2, statement3 will not be executed.

If the exception is not caught, will statement4be executed?

Answer : Yes statement4 will be executed, if the exception is not caught. In a try catch block, if there is no catch, then control goes to finally block directly

If the exception is not caught, will statement5be executed?

   Answer : Yes statement5 will be executed, if the exception is not caught. Since statement5 is after finally block.

If the exception is caught in the catch block, will statement4 be executed?

Answer :   Yes statement4 will be executed, if the exception is caught. In a try catch block, control goes to finally block after catch statement is executed.

If the exception is caught in the catch block, will statement5 be executed?

   Answer : Yes statement5 will be executed, if the exception is caught. In a try catch block, control goes to finally block after catch statement is executed and remaining statements after finally will be executed.

If the exception is of type Exception2, will statement4 be executed?

Answer : Yes statement4 will be executed, if the exception is of type exception2. In a try catch block, control goes to finally block after catch statement is executed.

If the exception is of type Exception2, will statement5 be executed?

   Answer : Yes statement5 will be executed, if the exception is of type exception2. In a try catch block, control goes to finally block after catch statement is executed.

If the exception is of type Exception3, will statement4 be executed?

Yes statement4 will be executed, if the exception is of type exception3. In a try catch block, control goes to finally block after catch statement is executed.

If the exception is of type Exception3, will statement5 be executed?

Yes statement5 will be executed, if the exception is of type exception3. In a try catch block, control goes to finally block after catch statement is executed.

Suppose that statement2 causes an exception in the following try-catch block: (10 points) public void m2() { m1(); } public void m1() { try { statement1; statem
Suppose that statement2 causes an exception in the following try-catch block: (10 points) public void m2() { m1(); } public void m1() { try { statement1; statem

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site