From 0904a24f30de1f0311aff17116c0c9e45a5f2772 Mon Sep 17 00:00:00 2001 From: Gayan Date: Wed, 7 Oct 2015 15:12:31 +0800 Subject: Adding exceptions and error handling --- php.html.markdown | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'php.html.markdown') 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). -- cgit v1.2.3 From a03b4003907e54da8c47c6aabe93c95f958b4f4d Mon Sep 17 00:00:00 2001 From: Gayan Date: Wed, 7 Oct 2015 15:13:17 +0800 Subject: Update php.html.markdown --- php.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 80e689b7..2de0d3fa 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -697,7 +697,7 @@ $cls = new SomeOtherNamespace\MyClass(); try { // Do something -catch ( Exception $e) { +} catch ( Exception $e) { // Handle exception } @@ -705,7 +705,7 @@ catch ( Exception $e) { try { // Do something -catch (\Exception $e) { +} catch (\Exception $e) { // Handle exception } -- cgit v1.2.3 From ddb3c9eab5f12a0942c9aab61a679d58043a483b Mon Sep 17 00:00:00 2001 From: Gayan Date: Wed, 7 Oct 2015 16:01:09 +0800 Subject: Update php.html.markdown --- php.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 2de0d3fa..7c796652 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -693,6 +693,11 @@ use My\Namespace as SomeOtherNamespace; $cls = new SomeOtherNamespace\MyClass(); +/********************** +* Error Handling +* +*/ + // Simple error handling can be done with try catch block try { -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 93066284..2b1fe1dc 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -515,7 +515,7 @@ class MyClass } } -// Class constants can always be accessed statically +// Class constants can always be accessed statically echo MyClass::MY_CONST; // Outputs 'value'; echo MyClass::$staticVar; // Outputs 'static'; -- cgit v1.2.3