Laravel 5.5新增了两个异常辅助函数: throw_ifthrow_unless, 都默认接收三个参数,第三个参数是可选的:

$number = 2;
throw_if($number !== 3, new NotThreeException('Number is not three'));
// or
throw_if($number !== 3, NotThreeException::class, 'Number is not three');

当错误代码$number不等于3的时候,就会抛出一个NotThreeException的异常,错误信息是Number is not three。而throw_unless则刚好是相反的,也即当第一个参数判断是false的时候,才会抛出异常

$number = 2;
throw_unless($number === 3, new NotThreeException('Number is not three'));
// or
throw_unless($number === 3, NotThreeException::class, 'Number is not three');