<?php function divide($a, $b) { if ($b == 0) { throw new Exception("Cannot divide by zero!"); } return $a / $b; } try { echo divide(10, 2); // valid echo "<br>"; echo divide(5, 0); // will throw exception } catch (Exception $e) { echo "Error: " . $e->getMessage(); } ?>