Very often we come across simple lines where we have to throw an exception if the precedent condition is not true.
for example:
public function actionView($id)
{
$model = User::find()->where(['id' => $id])->one();
if($model) {
throw new NotFoundHttpException('The user does not exist');
}
...
}
But i think it would be better if we could throw the exception on the same line as the query.
Example:
public function actionView($id)
{
$model = User::find()->where(['id' => $id])->one()
or NotFoundHttpException::throw('The user does not exist');
...
}
It's less clear as to me. Not going to change framework code style in this regard.
Thanks for opinion though.
thats is interesting. Havent seen this approach yet.
It is significant where exception is thrown. Exception object stores the stacktrace to the line, where it was thrown.
If throw operator will be moved inside static method, this stacktrace will show incorrect path, eliminating its benefit.
ahh didnt see that change too. I meant the 'or' logical operator.
Most helpful comment
It is significant where exception is thrown. Exception object stores the stacktrace to the line, where it was thrown.
If
throwoperator will be moved inside static method, this stacktrace will show incorrect path, eliminating its benefit.