Skip to content

Instantly share code, notes, and snippets.

@gustawdaniel
Created March 28, 2017 11:10
Show Gist options
  • Save gustawdaniel/96026a476abcb75f5d71609aeca7c8ac to your computer and use it in GitHub Desktop.
Save gustawdaniel/96026a476abcb75f5d71609aeca7c8ac to your computer and use it in GitHub Desktop.
zadanie2
<?php
function divide($divider, $dividend) {
if ($divider == 0){
throw new InvalidArgumentException("Divide by zero error");
}
if ($divider < 0){
throw new OutOfRangeException ("Divide is lover then zero");
}
return $dividend / $divider;
}
function randomDivide($tryNumber){
$invalidCounter = 0;
$outCounter = 0;
echo "---------------\n";
for ($n = 0; $n < $tryNumber; $n++){
try {
echo divide(rand(-10, 10), 5) . "\n";
} catch (InvalidArgumentException $e) {
$invalidCounter++;
} catch (OutOfRangeException $e) {
$outCounter++;
}
}
echo "---------------\n";
echo "| out: " . $outCounter . "\n";
echo "| inv: " . $invalidCounter . "\n";
echo "---------------\n";
}
randomDivide(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment