在这种情况下我应该重新抛出异常吗?
这个方法可以吗?我是否正确处理异常?看我的课:
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.');
}
}
}
Is this approach ok? 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.');
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不打算对该 catch 块中的异常执行任何操作,则无需将该父方法调用包含在其自己的 try-catch 块中。如果该方法在 try-catch 块之外遇到异常,它会自动将异常从父级实现向上传递,就像您从同一上下文抛出异常一样(就像在 if 条件之后所做的那样):
If you are not going to do anything with the exception within that catch block, it's not necessary to enclose that parent method call in its own try-catch block. The method will automatically pass the exception up from the parent's implementation if it encounters one outside a try-catch block, just like if you threw an exception from the same context (as you do after your if condition):