-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_exceptions.php
More file actions
32 lines (26 loc) · 784 Bytes
/
Copy path16_exceptions.php
File metadata and controls
32 lines (26 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/* ----------- Exceptions ----------- */
/*
PHP 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 or finally block.
*/
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1 / $x;
}
try {
echo inverse(5) . '<br>';
echo inverse(0);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
} finally {
echo "First finally.\n";
}
try {
echo inverse(0);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
} finally {
echo "Second finally.\n";
}