mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-08 07:46:46 +02:00
Merge pull request #1385 from gayanhewa/master
[php/en] Adding error handling
This commit is contained in:
@@ -693,8 +693,43 @@ use My\Namespace as SomeOtherNamespace;
|
|||||||
|
|
||||||
$cls = new SomeOtherNamespace\MyClass();
|
$cls = new SomeOtherNamespace\MyClass();
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* Error Handling
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## More Information
|
## More Information
|
||||||
|
Reference in New Issue
Block a user