mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-22 21:52:31 +03:00
Adding exceptions and error handling
This commit is contained in:
parent
d6f45adb94
commit
0904a24f30
@ -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
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user