21

Consider these two examples

<?php
function throw_exception() {
    // Arbitrary code here
    throw new Exception('Hello, Joe!');
}

function some_code() {
    // Arbitrary code here
}

try {
    throw_exception();
} catch (Exception $e) {
    echo $e->getMessage();
}

some_code();

// More arbitrary code
?>

and

<?php
function throw_exception() {
    // Arbitrary code here
    throw new Exception('Hello, Joe!');
}

function some_code() {
    // Arbitrary code here
}

try {
    throw_exception();
} catch (Exception $e) {
    echo $e->getMessage();
} finally {
    some_code();
}

// More arbitrary code
?>

What's the difference? Is there a situation where the first example wouldn't execute some_code(), but the second would? Am I missing the point entirely?

hakre
  • 193,403
  • 52
  • 435
  • 836
marxjohnson
  • 830
  • 7
  • 21
  • 4
    `Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.` – Dave Chen Jun 25 '13 at 08:52
  • I think this is related on this thread: http://stackoverflow.com/questions/15031515/can-i-use-try-catch-finally-like-this – JunM Jun 25 '13 at 08:53
  • 1
    Just to save anyone else copy-pasting from the PHP manual, I have read that but don't understand the difference between these two examples, otherwise I wouldn't have asked the question. – marxjohnson Jun 25 '13 at 08:56
  • See the RFC doc: https://wiki.php.net/rfc/finally – AKS Jun 25 '13 at 08:57
  • *sidenote:* finally block is available at PHP5.5+ – Raptor Aug 22 '14 at 08:54

5 Answers5

43

If you catch Exception (any exception) the two code samples are equivalent. But if you only handle some specific exception type in your class block and another kind of exception occurs, then some_code(); will only be executed if you have a finally block.

try {
    throw_exception();
} catch (ExceptionTypeA $e) {
    echo $e->getMessage();
}

some_code(); // Will not execute if throw_exception throws an ExceptionTypeB

but:

try {
    throw_exception();
} catch (ExceptionTypeA $e) {
    echo $e->getMessage();
} finally {
    some_code(); // Will be execute even if throw_exception throws an ExceptionTypeB
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
Simon
  • 6,293
  • 2
  • 28
  • 34
  • 1
    Worth a +1 for citing a specific instance when `some_code()` will not be executed – Mark Baker Jun 25 '13 at 09:02
  • I have spent a while researching why they are at all useful, and finally (no pun intended), your answer gives a valid reasoning for the necessity of a finally block. – Jeffrey Cordero Jul 21 '16 at 20:15
0

fianlly block is used when you want a piece of code to execute regardless of whether an exception occurred or not...

Check out Example 2 on this page :

PHP manual

Ashrith Sheshan
  • 654
  • 4
  • 17
0

Finally will trigger even if no exception were caught.

Try this code to see why:

<?php
class Exep1 extends Exception {}
class Exep2 extends Exception {}

try {
  echo 'try ';
  throw new Exep1();
} catch ( Exep2 $e)
{
  echo ' catch ';
} finally {
  echo ' finally ';
}

echo 'aftermath';

?>

the output will be

try  finally 
Fatal error: Uncaught exception 'Exep1' in /tmp/execpad-70360fffa35e/source-70360fffa35e:7
Stack trace:
#0 {main}
  thrown in /tmp/execpad-70360fffa35e/source-70360fffa35e on line 7

here is fiddle for you. https://eval.in/933947

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
0

From the PHP manual:

In PHP 5.5 and later, a finally block may also be specified after or instead of catch blocks. Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.

See this example in the manual, to see how it works.

MAChitgarha
  • 3,728
  • 2
  • 33
  • 40
-2

http://www.youtube.com/watch?v=EWj60p8esD0

Watch from: 12:30 onwards

Watch this video. The language is JAVA though. But i think it illustrates Exceptions and the use of finally keyword very well.

sodhancha
  • 435
  • 15
  • 31
  • Thanks soden, that explains what I already know, i.e. that it runs regardless of whether there's an exception. But so does code placed after the block, doesn't it? Is there a specific advantage to having it as part of the block? – marxjohnson Jun 25 '13 at 08:59
  • Depends on how much you want the exception to be abstracted from the user. Generally speaking using finally is a rarity. – sodhancha Jun 25 '13 at 09:04