What is the purpose of using try-catch blocks in PHP?

The purpose of using try-catch blocks in PHP is to handle exceptions that may occur during the execution of a block of code. By using try-catch blocks, you can catch and handle any exceptions that are thrown within the try block, preventing them from causing the script to crash.

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