「PHP」throw文で例外処理をするサンプル
書式
try {処理コード}catch(Exception $e)
サンプルコード
<?php
$cftA = 15;
$cftB = 0;
try{
echo testFunc($cftA,$cftB);
}catch(Exception $e){
echo $e->getMessage();
}finally{
echo "finish end";
}
function testFunc($x,$y){
if( $y == 0 ){
throw new Exception("erroe message 1234345");
}
return $x/$y;
}
?>
<?php
$cftA = 15;
$cftB = 0;
try{
echo testFunc($cftA,$cftB);
}catch(Exception $e){
echo $e->getMessage();
}finally{
echo "finish end";
}
function testFunc($x,$y){
if( $y == 0 ){
throw new Exception("erroe message 1234345");
}
return $x/$y;
}
?>
<?php $cftA = 15; $cftB = 0; try{ echo testFunc($cftA,$cftB); }catch(Exception $e){ echo $e->getMessage(); }finally{ echo "finish end"; } function testFunc($x,$y){ if( $y == 0 ){ throw new Exception("erroe message 1234345"); } return $x/$y; } ?>