You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/exceptions-and-error-handling.md
+18-18Lines changed: 18 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -8,18 +8,18 @@ Errors in PHP can be detected with a `try` block and a `catch` block. The code i
8
8
Exceptions can be thrown manually with the `throw` keyword. Since exceptions [are classes](https://secure.php.net/manual/en/class.exception.php) in PHP, you can still access all their inherited methods and properties.
9
9
```php
10
10
<?php
11
-
function foo($bar) {
12
-
if($bar == "baz") {
13
-
throw new Exception("Bar is equal to baz!");
11
+
function foo($bar) {
12
+
if($bar == "baz") {
13
+
throw new Exception("Bar is equal to baz!");
14
+
}
15
+
return true;
14
16
}
15
-
return true;
16
-
}
17
17
18
-
try {
19
-
foo("baz");
20
-
catch(Exception $e) {
21
-
print "An exception was caught: ".$e->getMessage();
22
-
}
18
+
try {
19
+
foo("baz");
20
+
} catch(Exception $e) {
21
+
print "An exception was caught: ".$e->getMessage();
22
+
}
23
23
?>
24
24
```
25
25
Will yield:
@@ -30,9 +30,9 @@ An exception was caught: Bar is equal to baz!
30
30
Custom exceptions can also be defined, since exceptions are objects:
exceptZeroDivisionError: // Catch only ZeroDivisionError exceptions
56
-
print("Divided by zero!")
56
+
print("Divided by zero!")
57
57
58
58
try:
59
-
10/0
59
+
10/0
60
60
except: // Catch any exception thrown
61
-
print("Some exception was thrown!")
61
+
print("Some exception was thrown!")
62
62
```
63
63
64
64
Exceptions can be manually thrown as well:
@@ -72,7 +72,7 @@ NameError
72
72
Python has many [built-in exceptions](https://docs.python.org/3/library/exceptions.html#bltin-exceptions), but you can also make your own by subclassing the `Exception` class:
0 commit comments