Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case where code never gets to clear() [duplicate]

Tags:

java

try {
    if (check) {
        while (true) ;
    } else {
        System.exit(1);
    }
} finally {
    clear();
}

Q: Is there a case where clear() never gets executed? I personally feel that there are no cases where clear() will not be executed.

like image 306
LaneLane Avatar asked Oct 07 '22 03:10

LaneLane


2 Answers

If System.exit succeeds, the clear() will not be executed. However, System.exit(1) could throw SecurityException exception, in which case clear() will be executed.

like image 101
Sergey Kalinichenko Avatar answered Oct 11 '22 04:10

Sergey Kalinichenko


If check is true, then you are stuck in an infinite loop, in which case you never technically get to the finally block.

Else if check is false, then System.exit(1); is executed and your program terminates; in which case it never gets to the finally block to execute clear().

In this example, it is actually not possible to reach clear()

like image 37
sampson-chen Avatar answered Oct 11 '22 03:10

sampson-chen