1

Is there any point in using the finally block in PHP? If it always runs no matter what, could you just not have it in the block? For example, these will output the same:

1.

try {
    echo 'Hello';
    switch(false) {
        case 1==2:
            throw new Exception('error');
            break;
    }
} catch(Exception $e) {
    echo $e->getMessage();
} finally {
    echo 'World';
}

2.

try {
    echo 'Hello';
    switch(false) {
        case 1==2:
            throw new Exception('error');
            break;
    }
} catch(Exception $e) {
    echo $e->getMessage();
}
echo 'World';

They'll both echo:

Hello
error
World

This confuses me Lol

Just Lucky Really
  • 1,341
  • 1
  • 15
  • 38
  • Good question, it's just been asked before. If anyone has a better answer, please post it there. – Wesley Murch May 22 '14 at 17:14
  • @WesleyMurch There's also another answer, which is that you might not nessesarily catch the exception, but you might still want to run the `finally` statement: [**DEMO**](https://eval.in/155810) – h2ooooooo May 22 '14 at 17:16
  • @h2ooooooo Ping me if you post an answer and I'll upvote it. You can post an answer that just covers what the other one did not, that is perfectly acceptable. – Wesley Murch May 22 '14 at 17:17
  • @WesleyMurch I've posted it as an answer on [the linked thread](http://stackoverflow.com/a/23813429/247893). – h2ooooooo May 22 '14 at 17:23
  • So ... What I kinda got from that, is that the `finally` clause is only useful if your code causes PHP to have a fatal error? – Just Lucky Really May 22 '14 at 17:37

0 Answers0