try..catch..finally
Syntax
try {
nejaký PHP kód
} catch (Exception $e) {
echo 'Chyba: ', $e->getMessage(), "
";
} finally {
echo "Či už chyba vznikla, alebo nie, tento text sa vypíše.
";
}
nejaký PHP kód
} catch (Exception $e) {
echo 'Chyba: ', $e->getMessage(), "
";
} finally {
echo "Či už chyba vznikla, alebo nie, tento text sa vypíše.
";
}
Popis
Príkaz jazyka PHP
PHP 5 has an exception model similar to that of other programming languages. An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions. Normal execution (when no exception is thrown within the try block, or when a catch matching the thrown exception's class is not present) will continue after that last catch block defined in sequence. Exceptions can be thrown (or re-thrown) within a catch block.
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." message, unless a handler has been defined with set_exception_handler().
In PHP 5.5 and later, a finally block may also be specified after the 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.
The thrown object must be an instance of the Exception class or a subclass of Exception. Trying to throw an object that is not will result in a PHP Fatal Error.
Príklad 1
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}
try {
echo inverse(5) . "
";
echo inverse(0) . "
";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "
";
}
// Continue execution
echo "Hello World
";
?>
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}
try {
echo inverse(5) . "
";
echo inverse(0) . "
";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "
";
}
// Continue execution
echo "Hello World
";
?>
Príklad vypíše toto:
0.2
Caught exception: Division by zero.
Hello World
Príklad 2
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}
try {
echo inverse(5) . "
";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "
";
} finally {
echo "First finally.
";
}
try {
echo inverse(0) . "
";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "
";
} finally {
echo "Second finally.
";
}
// Continue execution
echo "Hello World
";
?>
Príklad vypíše toto:
0.2
First finally.
Caught exception: Division by zero.
Second finally.
Hello World
Príklad 3
<?php
class MyException extends Exception { }
class Test {
public function testing() {
try {
try {
throw new MyException('foo!');
} catch (MyException $e) {
// rethrow it
throw $e;
}
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
}
$foo = new Test;
$foo->testing();
?>
Príklad vypíše toto:
string(4) "foo!"