What are the potential pitfalls of using die() in PHP scripts, and what are alternative methods to handle errors more gracefully?

Using die() in PHP scripts can abruptly terminate the script execution, making it difficult to handle errors gracefully. Instead, it's better to use exceptions or error handling functions like try, catch, and throw to handle errors more effectively.

try {
    // code that may throw an exception
    throw new Exception("An error occurred.");
} catch (Exception $e) {
    // handle the exception gracefully
    echo "Error: " . $e->getMessage();
}