Should I rebuild the exception in this case?

Is this approach appropriate? Am I handling exceptions correctly? See My class:

class Email extends String
{
protected function validate($email)
{
    try{
        parent::validate($email);
    } catch(InvalidArgumentException $e) {
        throw $e;
    }

    if(!filter_var($value,FILTER_VALIDATE_EMAIL))
    {
        throw new InvalidArgumentException('etc.');
    }
}
}
+3
source share
1 answer

If you are not going to do anything with an exception in this catch block, there is no need to enclose the call to the parent method in your own try-catch block. The method automatically passes an exception from the parent implementation if it encounters one outside the try-catch block, just as if you had selected an exception from the same context (as after the if condition):

protected function validate($email)
{
    parent::validate($email);

    if (!filter_var($value, FILTER_VALIDATE_EMAIL))
    {
        throw new InvalidArgumentException('etc.');
    }
}
+11
source

Source: https://habr.com/ru/post/1791715/


All Articles