<?php function divide($a, $b) { if ($b === 0) { throw new Exception("Cannot divide by zero."); } return $a / $b; } try { echo divide(10, 2); // 5 echo divide(10, 0); // throws } catch (Exception $e) { echo "Error: " . $e->getMessage(); } ?>
Click Run to execute this code.