amitmerchant1990 / amitmerchant-dot-com-comments

1 stars 0 forks source link

using-custom-exception-php/ #14

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Benefits of using custom exceptions in PHP – Amit Merchant – A blog on PHP, JavaScript and more

Exceptions are really useful when you want to handle some situations which can not be handled gracefully otherwise. So, using exceptions, you can handle certain sceanrios by showing a nice error message. Take the following example for instance.

https://www.amitmerchant.com/using-custom-exception-php/

Kwaadpepper commented 3 years ago

Really Nice blog, as an advice and I think it's a common php usage, don't put php end tags on pages '?>', this will prevent sending new lines and unwanted pieces of code of strings.

Kwaadpepper commented 3 years ago

Also using you technique may prevent filtering Exceptions Types in try catch

try {
    // piece of code
} catch(Exception | TypeError $e) {
    echo 'Critical Failure';
} catch(IsUser $e) {
    echo 'we can only filter on IsUser exception type';
}

see you cannot filter notAllowed or toEmailNull using your technique.

How ever this pattern is still sweet :)