Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Try, Catch, and Finally | Exception Handling and Customization
Practice
Projects
Quizzes & Challenges
Quizzer
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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookTry, Catch, and Finally

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1
some-alt