Blok try...catch menangkap kegagalan koneksi database atau operasi file tanpa mengekspos pesan error sistem internal ke publik.
PHP Exceptions
Contoh Kode PHP Exceptions:
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px;">
<h2>Blok try...catch Exception</h2>
<div style="background:#f8fafc; border:1px solid #cbd5e1; padding:15px; border-radius:8px;">
<?php<br>
try {<br>
throw new Exception("Koneksi gagal");<br>
} catch (Exception $e) {<br>
echo "Tertangkap error: " . $e->getMessage();<br>
}<br>
?>
</div>
</body>
</html>
Uji kode PHP dan amati simulasi output server pada panel pratinjau di atas.