Try, Catch, and Finally
Exception handling in PHP gives you a structured way to deal with errors that may occur during program execution. The main tools for this are the try, catch, and finally blocks. You use a try block to wrap code that might throw an exception. If an exception occurs, control moves immediately to the corresponding catch block, where you can handle the error safely. The finally block contains code that will always run after the try and catch blocks, regardless of whether an exception was thrown or not. This is especially useful for cleanup operations, such as closing files or releasing resources.
example.php
123456789101112131415161718<?php function divide($a, $b) { if ($b === 0) { throw new Exception("Division by zero is not allowed."); } return $a / $b; } $result = null; try { $result = divide(10, 0); echo "Result: $result\n"; } catch (Exception $e) { echo "Caught exception: " . $e->getMessage() . "\n"; } finally { echo "Cleaning up resources...\n"; }
In the code above, the divide function checks if the divisor is zero and throws an Exception if it is. The call to divide(10, 0) inside the try block results in an exception being thrown. When this happens, the rest of the code in the try block is skipped, and control jumps to the catch block, which prints the exception message. After the catch block executes, the finally block runs, printing a cleanup message. If no exception had occurred, the finally block would still execute. This flow ensures that cleanup code always runs, regardless of whether an error happened, making your programs more reliable and maintainable.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 8.33
Try, Catch, and Finally
Свайпніть щоб показати меню
Exception handling in PHP gives you a structured way to deal with errors that may occur during program execution. The main tools for this are the try, catch, and finally blocks. You use a try block to wrap code that might throw an exception. If an exception occurs, control moves immediately to the corresponding catch block, where you can handle the error safely. The finally block contains code that will always run after the try and catch blocks, regardless of whether an exception was thrown or not. This is especially useful for cleanup operations, such as closing files or releasing resources.
example.php
123456789101112131415161718<?php function divide($a, $b) { if ($b === 0) { throw new Exception("Division by zero is not allowed."); } return $a / $b; } $result = null; try { $result = divide(10, 0); echo "Result: $result\n"; } catch (Exception $e) { echo "Caught exception: " . $e->getMessage() . "\n"; } finally { echo "Cleaning up resources...\n"; }
In the code above, the divide function checks if the divisor is zero and throws an Exception if it is. The call to divide(10, 0) inside the try block results in an exception being thrown. When this happens, the rest of the code in the try block is skipped, and control jumps to the catch block, which prints the exception message. After the catch block executes, the finally block runs, printing a cleanup message. If no exception had occurred, the finally block would still execute. This flow ensures that cleanup code always runs, regardless of whether an error happened, making your programs more reliable and maintainable.
Дякуємо за ваш відгук!