0
0
PHPprogramming~20 mins

Error vs Exception in PHP - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Error vs Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of error and exception handling
What will be the output of this PHP code snippet?
PHP
<?php
function test() {
    try {
        throw new Exception("Exception caught");
    } catch (Error $e) {
        echo "Error caught";
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
test();
AError caught
BFatal error
CException caught
DNo output
Attempts:
2 left
💡 Hint
Remember that Exception and Error are different classes in PHP and catch blocks handle exceptions based on type.
Predict Output
intermediate
2:00remaining
Behavior of uncaught Error
What happens when this PHP code runs?
PHP
<?php
function test() {
    try {
        undefinedFunction();
    } catch (Exception $e) {
        echo "Exception caught";
    }
}
test();
AWarning but script continues
BException caught
CNo output
DFatal error: Uncaught Error: Call to undefined function
Attempts:
2 left
💡 Hint
Undefined functions cause Errors, not Exceptions.
🔧 Debug
advanced
2:00remaining
Identify the error type caught
What type of error or exception is caught by this code?
PHP
<?php
try {
    $arr = [];
    $arr[5]();
} catch (Error $e) {
    echo get_class($e);
} catch (Exception $e) {
    echo get_class($e);
}
AErrorException
BError
CException
DUndefinedOffsetException
Attempts:
2 left
💡 Hint
Accessing an undefined array index triggers a specific error type in PHP 7+.
📝 Syntax
advanced
2:00remaining
Which catch block order is correct?
Given that Error and Exception are both throwable in PHP, which catch block order will NOT cause a fatal error?
PHP
try {
    // some code
} catch (___) {
    // handle
} catch (___) {
    // handle
}
Acatch (Error $e) then catch (Exception $e)
Bcatch (Exception $e) then catch (Error $e)
Ccatch (Exception $e) then catch (Throwable $t)
Dcatch (Throwable $t) then catch (Exception $e)
Attempts:
2 left
💡 Hint
More specific exceptions must be caught before more general ones.
🧠 Conceptual
expert
2:00remaining
Difference between Error and Exception in PHP
Which statement best describes the difference between Error and Exception in PHP?
AErrors represent serious problems from PHP engine or environment, Exceptions represent user-defined or runtime issues that can be caught and handled.
BErrors and Exceptions are identical and interchangeable in PHP.
CErrors represent fatal problems that cannot be caught, Exceptions represent recoverable problems that can be caught.
DExceptions are only for syntax errors, Errors are for runtime errors.
Attempts:
2 left
💡 Hint
Think about what kind of problems each class represents and how PHP treats them.