Source Code:
(back to article)
<?php // Example 1 function divide($numerator, $denominator) { if ($denominator == 0) { throw new Exception("Cannot divide by zero." . PHP_EOL); } return $numerator / $denominator; } try { $result = divide(10, 0); echo "Result: " . $result; } catch (Exception $e) { echo "Caught exception: " . $e->getMessage(); } // Example 2 class CustomException extends Exception { } function testException() { throw new CustomException("This is a custom exception."); } try { testException(); } catch (CustomException $e) { echo "Caught custom exception: " . $e->getMessage(); }
Result:
Report an issue