parent
ec836bcb31
commit
4162b79113
9 changed files with 183 additions and 12 deletions
@ -0,0 +1,15 @@ |
||||
<?php |
||||
function inverse($x) { |
||||
return number_format(1.0 / $x, 2); |
||||
} |
||||
|
||||
function main() { |
||||
try { |
||||
echo inverse(5.0) . "\n"; |
||||
echo inverse(0) . "\n"; |
||||
} catch (DivisionByZeroError $e) { |
||||
echo 'Caught exception: ', $e->getMessage(), "\n"; |
||||
} finally { |
||||
echo "Finally\n"; |
||||
} |
||||
} |
||||
@ -0,0 +1,23 @@ |
||||
--TEST-- |
||||
try catch |
||||
--FILE-- |
||||
<?php |
||||
function inverse($x) { |
||||
return number_format(1.0 / $x, 2); |
||||
} |
||||
|
||||
function main() { |
||||
try { |
||||
echo inverse(5.0) . "\n"; |
||||
echo inverse(0) . "\n"; |
||||
} catch (DivisionByZeroError $e) { |
||||
echo 'Caught exception: ', $e->getMessage(), "\n"; |
||||
} finally { |
||||
echo "Finally\n"; |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
0.20 |
||||
Caught exception: Division by zero |
||||
Finally |
||||
@ -0,0 +1,28 @@ |
||||
--TEST-- |
||||
try catch |
||||
--FILE-- |
||||
<?php |
||||
function inverse($x) { |
||||
if (!$x) { |
||||
throw new Exception('Division by zero.'); |
||||
} |
||||
return number_format(1.0 / $x, 2); |
||||
} |
||||
|
||||
function main() { |
||||
try { |
||||
echo inverse(5.0) . "\n"; |
||||
echo inverse(0) . "\n"; |
||||
} catch (Exception $e) { |
||||
echo 'Caught exception: ', $e->getMessage(), "\n"; |
||||
} catch (RuntimeException $e) { |
||||
echo 'Caught runtime exception: ', $e->getMessage(), "\n"; |
||||
} finally { |
||||
echo "Finally\n"; |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
0.20 |
||||
Caught exception: Division by zero. |
||||
Finally |
||||
Loading…
Reference in new issue