|
| 1 | +[<-- Return to index](../README.md) |
| 2 | +# Errors and Exception Handling |
| 3 | + |
| 4 | +### How are errors/exceptions treated? |
| 5 | +#### PHP |
| 6 | +Errors in PHP can be detected with a `try` block and a `catch` block. The code in the `try` block will be attempted and if any errors are encountered, the code in the `catch` block will execute. If there were no errors in the `try` code execution, the code in the `catch` block will be skipped. |
| 7 | + |
| 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 | +```php |
| 10 | +<?php |
| 11 | +function foo($bar) { |
| 12 | + if($bar == "baz") { |
| 13 | + throw new Exception("Bar is equal to baz!"); |
| 14 | + } |
| 15 | + return true; |
| 16 | +} |
| 17 | + |
| 18 | +try { |
| 19 | + foo("baz"); |
| 20 | +catch(Exception $e) { |
| 21 | + print "An exception was caught: ".$e->getMessage(); |
| 22 | +} |
| 23 | +?> |
| 24 | +``` |
| 25 | +Will yield: |
| 26 | +``` |
| 27 | +An exception was caught: Bar is equal to baz! |
| 28 | +``` |
| 29 | + |
| 30 | +Custom exceptions can also be defined, since exceptions are objects: |
| 31 | +```php |
| 32 | +<?php |
| 33 | + class myError extends Exception { |
| 34 | + public function reportError() { |
| 35 | + return "There was an error!"; |
| 36 | + } |
| 37 | + } |
| 38 | +?> |
| 39 | +``` |
| 40 | +#### Python |
| 41 | +Python differentiates between syntax errors and exceptions. Syntax errors raise at execution time and as one would expect only raise when there is invalid structure to the Python code. |
| 42 | + |
| 43 | +Exceptions are raised when an error is encountered at some point during code execution. Exceptions can be caught using the `except` keyword. in a `try` block. If the code in a `try` block encounters any error, the code in the `except` block will be executed. Otherwise, the code in the `except` block will be skipped. |
| 44 | + |
| 45 | +The `try` block can match a specific exception or catch all exceptions. If an exception is raised that is not handled by the `try` block, the exception will raise uncaught and terminate the program execution. |
| 46 | + |
| 47 | +```python |
| 48 | +>>> 10/0 // An uncaught exception |
| 49 | +Traceback (most recent call last): |
| 50 | + File "<stdin>", line 1, in <module> |
| 51 | +ZeroDivisionError: division by zero |
| 52 | + |
| 53 | +try: |
| 54 | + 10/0 |
| 55 | +except ZeroDivisionError: // Catch only ZeroDivisionError exceptions |
| 56 | + print("Divided by zero!") |
| 57 | + |
| 58 | +try: |
| 59 | + 10/0 |
| 60 | +except: // Catch any exception thrown |
| 61 | + print("Some exception was thrown!") |
| 62 | +``` |
| 63 | + |
| 64 | +Exceptions can be manually thrown as well: |
| 65 | +```python |
| 66 | +>>>raise NameError |
| 67 | +Traceback (most recent call last): |
| 68 | + File "<stdin>", line 1, in <module> |
| 69 | +NameError |
| 70 | +``` |
| 71 | + |
| 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: |
| 73 | +```python |
| 74 | +class myError(Exception): |
| 75 | + pass |
| 76 | + |
| 77 | +>>>raise myError |
| 78 | +Traceback (most recent call last): |
| 79 | + File "<stdin>", line 1, in <module> |
| 80 | + __main__.myError |
| 81 | +``` |
0 commit comments