try { $file = fopen("myfile.txt", "r"); if (!$file) { throw new Exception("File not found", 404); } } catch (Exception $e) { echo "Error: " . $e->getMessage() . " (code " . $e->getCode() . ")"; }
try { $numerator = 10; $denominator = 0; if ($denominator == 0) { throw new Exception("Cannot divide by zero", 500); } $result = $numerator / $denominator; } catch (Exception $e) { echo "Error: " . $e->getMessage() . " (code " . $e->getCode() . ")"; }The Exception class and its getCode() function are part of the core PHP library.