如何处理方法抛出的异常?该方法必须在方法“ check”中没有引发异常
<?php
class AllAccidents
{
public static function check() {
try {
$x = 1;
if($x)
throw new Exception("Value must be more than 1");
}catch (Exception $e){
echo "hello>>".$e->getMessage();
}
}
}
class Test
{
public function go(){
try{
AllAccidents::check();
} catch (Exception $e){
}
}
}
$obj = new Test();
$obj->go();
?>
我已经格式化了您的代码,可以在想要引发异常时设置逻辑
<?php
class AllAccidents
{
public static function check() {
try {
self::checkNum(2);
}catch (Exception $e){
echo $e->getMessage();
}
}
public static function checkNum($number) {
if($number>1) {
throw new Exception("Value must be 1 or below");
}
return true;
}
}
class Test
{
public function go(){
try{
AllAccidents::check();
} catch (Exception $e){
}
}
}
$obj = new Test();
$obj->go();
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句