“Exceptions PHP” Réponses codées

Exceptions PHP

<?php
  
  #We use exceptions to handle errors in PHP
  
  function divide($dividend, $divisor) {
  	if($divisor == 0) {
    	throw new Exception("Division by zero");
  	}
  return $dividend / $divisor;
}

echo divide(5, 0);
  
?>
Rick Astley

exception de catch PHP


<?php
function inverse($x) {
    if (!$x) {
       throw new Exception('Division durch Null.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
}

// Ausführung fortsetzen
echo "Hallo Welt\n";
?>

Vadris_

PHP attrape toutes les exceptions

try {
  // call a success/error/progress handler
} catch (\Throwable $e) { // For PHP 7
  // handle $e
} catch (\Exception $e) { // For PHP 5
  // handle $e
}
portapipe

Exceptions PHP

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

echo divide(5, 0);
?>
naly moslih

Réponses similaires à “Exceptions PHP”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code