PHP Exceptions
From Wiki
Exceptions use the try, catch syntax similar to Java or Python, although programmers using those languages will note the absence of finally.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
function printHeader($title, $keywords, $description) {
if(strlen($description) < 40)
throw new Exception("A reasonable description length is required");
print("<html xmlns='http://www.w3.org/1999/xhtml'><head>");
print("<title>$title</title>");
print("<meta name=\"Keywords\" content=\"$keywords \"/>");
print("<meta name=\"Description\" content=\"$description \"/>");
print("</head><body><p>");
}
try {
print_header("My Page",'PHP, Programming, Beer','');
}catch (Exception $e) {
echo($e->getMessage());
}
?>
</p>
</body>
</html>
Instead of simply calling our function, we've enclosed the function in a new control structure, the try...catch block. If we execute the code as written, PHP first tries to execute the function as described; then it terminates
execution almost immediately, because the $description variable has failed our simple test. At this point, the
script can continue execution after the try...catch block, or it can be terminated with die() or exit().
Multiple exceptions can be defined in a single function. This is good idea because it yields more specific information about what exactly happened.
Because execution stops with the first exception, only this exception will be caught.
