How can try/catch blocks be effectively used to handle exceptions in PHP?

Try/catch blocks in PHP can be effectively used to handle exceptions by enclosing the code that may throw an exception within a try block. If an exception is thrown within the try block, it can be caught and handled in the catch block. This allows for graceful error handling and prevents the script from crashing.

try {
    // Code that may throw an exception
    $result = 10 / 0;
} catch (Exception $e) {
    // Handle the exception
    echo "An error occurred: " . $e->getMessage();
}