1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
<?php function sum($val1, $val2) { //変数の肩が数値型出ないときに例外を返す if (getType($val1) !== "integer" && getType($val1) !== "double") { throw new Exception('$val1: 数値以外の型'); } if (getType($val2) !== "integer" && getType($val2) !== "double") { throw new Exception('$val2: 数値以外の型'); } } try { $result = sum(5, '2'); } catch (Exception $e) { echo $e->getMessage(); } |
ポイント 関数サイド 例外を返したい箇所に、 ...