「PHP」throw文で例外処理をするサンプル

書式
try {処理コード}catch(Exception $e)
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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;
}
?>

 

PHP

Posted by arkgame