0
0
PHPprogramming~20 mins

Why error handling matters in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code with error handling?

Look at this PHP code that tries to divide two numbers. What will it print?

PHP
<?php
function divide($a, $b) {
    try {
        if ($b == 0) {
            throw new Exception("Division by zero.");
        }
        return $a / $b;
    } catch (Exception $e) {
        return $e->getMessage();
    }
}

echo divide(10, 0);
AFatal error: Division by zero
BDivision by zero.
C0
D10
Attempts:
2 left
💡 Hint

Think about what happens when the divisor is zero and how the exception is handled.

Predict Output
intermediate
2:00remaining
What happens if error handling is missing?

What will this PHP code output?

PHP
<?php
function divide($a, $b) {
    return $a / $b;
}

echo divide(10, 0);
AFatal error: Division by zero
BDivision by zero.
C0
D10
Attempts:
2 left
💡 Hint

What happens when you divide by zero in PHP without catching errors?

🔧 Debug
advanced
3:00remaining
Find the error in this PHP error handling code

There is a mistake in this PHP code that tries to handle errors. What is it?

PHP
<?php
function readFileContent($filename) {
    try {
        $content = file_get_contents($filename);
        if (!$content) {
            throw new Exception("File not found or empty.");
        }
        return $content;
    } catch (Exception $e) {
        return $e->getMessage();
    }
}

echo readFileContent("missing.txt");
AThe Exception class is not imported.
BThe try block is missing a catch block.
CThe condition if (!$content) fails when file content is '0', causing false error.
Dfile_get_contents throws a fatal error, so try-catch won't work.
Attempts:
2 left
💡 Hint

Think about what happens if the file content is the string '0'.

🧠 Conceptual
advanced
2:00remaining
Why is error handling important in PHP applications?

Choose the best reason why error handling is important in PHP applications.

AIt prevents the application from crashing and allows graceful recovery.
BIt makes the code run faster by skipping error checks.
CIt hides all errors so users never see any messages.
DIt automatically fixes bugs in the code.
Attempts:
2 left
💡 Hint

Think about what happens when something unexpected occurs in your program.

Predict Output
expert
3:00remaining
What is the output of this PHP code with nested try-catch?

Analyze this PHP code with nested try-catch blocks. What will it output?

PHP
<?php
function test() {
    try {
        try {
            throw new Exception("Inner error");
        } catch (Exception $e) {
            echo "Caught: " . $e->getMessage() . "\n";
            throw new Exception("Outer error");
        }
    } catch (Exception $e) {
        echo "Caught again: " . $e->getMessage() . "\n";
    }
}

test();
AFatal error: Uncaught Exception: Outer error
B
Caught: Outer error
Caught again: Inner error
C
Caught: Inner error
D
Caught: Inner error
Caught again: Outer error
Attempts:
2 left
💡 Hint

Look carefully at how exceptions are re-thrown and caught in nested blocks.