Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Try, Catch, and Finally | Exception Handling and Customization
Practice
Projects
Quizzes & Challenges
Questionários
Challenges
/
PHP Error Handling

bookTry, 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

example.php

copy
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.

question mark

What is the main purpose of the finally block in PHP exception handling?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookTry, Catch, and Finally

Deslize para mostrar o menu

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

example.php

copy
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.

question mark

What is the main purpose of the finally block in PHP exception handling?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
some-alt