PHP - Statement finally block

Introduction

The finally block is added after the try...catch one, and it is optional.

The catch block is optional too.

A try statement must be followed by at least one of them.

So you could have these three scenarios:

scenario 1: the whole try-catch-finally

try { 
    // code that might throw an exception 
 } catch (Exception $e) { 
    // code that deals with the exception 
 } finally { 
    // finally block 
 } 

scenario 2: try-finally without catch

try { 
    // code that might throw an exception 
 } finally { 
    // finally block 
 } 

scenario 3: try-catch without finally

try { 
   // code that might throw an exception 
} catch (Exception $e) { 
   // code that deals with the exception 
} 

The finally block is executed when either the try or the catch blocks are executed completely.

The finally block is executed regardless.