summaryrefslogtreecommitdiffhomepage
path: root/php.html.markdown
diff options
context:
space:
mode:
authorGayan <gayanhewa@gmail.com>2015-10-07 15:12:31 +0800
committerGayan <gayanhewa@gmail.com>2015-10-07 15:12:31 +0800
commit0904a24f30de1f0311aff17116c0c9e45a5f2772 (patch)
treea421c5348cfe8b6186801585cc57d07f16d5611a /php.html.markdown
parentd6f45adb94a3e7a16896c5301199baf8a96f6754 (diff)
Adding exceptions and error handling
Diffstat (limited to 'php.html.markdown')
-rw-r--r--php.html.markdown34
1 files changed, 32 insertions, 2 deletions
diff --git a/php.html.markdown b/php.html.markdown
index 93066284..80e689b7 100644
--- a/php.html.markdown
+++ b/php.html.markdown
@@ -693,7 +693,37 @@ use My\Namespace as SomeOtherNamespace;
$cls = new SomeOtherNamespace\MyClass();
-*/
+// Simple error handling can be done with try catch block
+
+try {
+ // Do something
+catch ( Exception $e) {
+ // Handle exception
+}
+
+// When using try catch blocks in a namespaced enviroment use the following
+
+try {
+ // Do something
+catch (\Exception $e) {
+ // Handle exception
+}
+
+// Custom exceptions
+
+class MyException extends Exception {}
+
+try {
+
+ $condition = true;
+
+ if ($condition) {
+ throw new MyException('Something just happend');
+ }
+
+} catch (MyException $e) {
+ // Handle my exception
+}
```
@@ -709,4 +739,4 @@ If you're coming from a language with good package management, check out
[Composer](http://getcomposer.org/).
For common standards, visit the PHP Framework Interoperability Group's
-[PSR standards](https://github.com/php-fig/fig-standards). \ No newline at end of file
+[PSR standards](https://github.com/php-fig/fig-standards).