Source Code:
(back to article)
<?php // Example 1 function divide($numerator, $denominator) { if ($denominator == 0) { throw new Exception("Division by zero."); } return $numerator / $denominator; } try { echo divide(10, 0); } catch (Exception $e) { echo "Error: " . $e->getMessage(); } // Output: Error: Division by zero. // Example 2 try { // Code that may throw an exception } catch (Exception1 $e1) { // Code to execute if Exception1 is thrown } catch (Exception2 $e2) { // Code to execute if Exception2 is thrown } catch (Exception $e) { // Code to execute if any other exception is thrown }
Result:
Report an issue