Bird
Raised Fist0
PHPprogramming~20 mins

Throwing exceptions in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Exception 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 exception?
Look at the code below. What will it output when run?
PHP
<?php
function test() {
    try {
        throw new Exception("Error happened");
    } catch (Exception $e) {
        return $e->getMessage();
    }
}
echo test();
?>
AException
BError happened
CFatal error
DNo output
Attempts:
2 left
💡 Hint
The catch block returns the exception message.
❓ Predict Output
intermediate
2:00remaining
What error does this PHP code raise?
What error will this code produce when run?
PHP
<?php
function example() {
    throw new Exception("Oops");
}
example();
echo "Done";
?>
AParse error
BDone
CNo output
DUncaught Exception: Oops
Attempts:
2 left
💡 Hint
There is no try-catch block to handle the exception.
🔧 Debug
advanced
2:00remaining
Why does this PHP code cause a fatal error?
This code tries to throw an exception but causes a fatal error. Why?
PHP
<?php
try {
    throw new Exception("Fail");
} catch (Error $e) {
    echo "Caught error";
}
?>
ABecause Exception class is undefined
BBecause catch block is empty
CBecause Exception is not caught by catch(Error $e)
DBecause throw syntax is wrong
Attempts:
2 left
💡 Hint
Exception and Error are different classes in PHP.
📝 Syntax
advanced
2:00remaining
Which option correctly throws and catches an exception?
Choose the code snippet that correctly throws and catches an exception in PHP.
A<?php try { throw new Exception("Error"); } catch (Exception $e) { echo $e->getMessage(); } ?>
B<?php try { throw new Exception("Error") } catch Exception $e { echo $e->getMessage(); } ?>
C<?php try { throw new Exception("Error"); } catch (Exception $e) echo $e->getMessage(); ?>
D<?php try { throw new Exception("Error"); } catch (Exception $e) { echo $e->message; } ?>
Attempts:
2 left
💡 Hint
Check for correct syntax: semicolons, parentheses, and method calls.
🚀 Application
expert
2:00remaining
How many items are in the array after exception handling?
What is the number of elements in the array $results after running this code?
PHP
<?php
$results = [];
try {
    $results[] = 1;
    throw new Exception("Stop");
    $results[] = 2;
} catch (Exception $e) {
    $results[] = 3;
}
$results[] = 4;
print_r($results);
?>
A3
B5
C2
D4
Attempts:
2 left
💡 Hint
Remember that code after throw inside try is skipped.

Practice

(1/5)
1. What does the throw statement do in PHP?
easy
A. It catches an exception to handle an error.
B. It creates and signals an exception to indicate an error.
C. It defines a new function for error handling.
D. It stops the script without any error message.

Solution

  1. Step 1: Understand the purpose of throw

    The throw statement is used to create and signal an exception when an error occurs.
  2. Step 2: Differentiate from other error handling keywords

    catch handles exceptions, but throw actually creates and signals them.
  3. Final Answer:

    It creates and signals an exception to indicate an error. -> Option B
  4. Quick Check:

    throw = signal error [OK]
Hint: Remember: throw = create error, catch = handle error [OK]
Common Mistakes:
  • Confusing throw with catch
  • Thinking throw stops script silently
  • Assuming throw defines functions
2. Which of the following is the correct syntax to throw an exception with the message "Error occurred" in PHP?
easy
A. throw new Exception("Error occurred");
B. throw Exception("Error occurred");
C. throw Exception new("Error occurred");
D. throw new Exception(Error occurred);

Solution

  1. Step 1: Recall the syntax for throwing exceptions

    In PHP, you must use throw new Exception("message") to throw an exception with a message.
  2. Step 2: Check each option for syntax correctness

    Only throw new Exception("Error occurred"); matches the correct syntax exactly. The other options either omit the new keyword, have the wrong order of keywords, or lack quotes around the message.
  3. Final Answer:

    throw new Exception("Error occurred"); -> Option A
  4. Quick Check:

    Correct syntax = throw new Exception(message) [OK]
Hint: Use 'throw new Exception("message")' exactly [OK]
Common Mistakes:
  • Omitting the 'new' keyword
  • Not quoting the message string
  • Wrong order of keywords
3. What will be the output of the following PHP code?
try {
    throw new Exception("Oops!");
    echo "No error";
} catch (Exception $e) {
    echo $e->getMessage();
}
medium
A. No error
B. Fatal error
C. Exception thrown but no output
D. Oops!

Solution

  1. Step 1: Analyze the try block

    The code throws an exception with message "Oops!" before the echo statement, so "No error" is never printed.
  2. Step 2: Understand the catch block behavior

    The catch block catches the exception and echoes the message from the exception, which is "Oops!".
  3. Final Answer:

    Oops! -> Option D
  4. Quick Check:

    Exception message printed = Oops! [OK]
Hint: Exception stops try block; catch prints message [OK]
Common Mistakes:
  • Expecting code after throw to run
  • Thinking no output appears
  • Confusing exception message with error
4. Identify the error in this PHP code snippet:
try {
    throw new Exception("Error!");
} catch (Exception $e) {
    echo $e->message();
}
medium
A. Exception class is not imported.
B. Missing semicolon after throw statement.
C. Method message() does not exist; should use getMessage().
D. Catch block syntax is incorrect.

Solution

  1. Step 1: Check the catch block method call

    The Exception class has a method getMessage() to get the error message, not message().
  2. Step 2: Verify other syntax elements

    The semicolon after throw is present, Exception class is built-in, and catch syntax is correct.
  3. Final Answer:

    Method message() does not exist; should use getMessage(). -> Option C
  4. Quick Check:

    Use getMessage() to get exception text [OK]
Hint: Use getMessage() method to read exception message [OK]
Common Mistakes:
  • Calling non-existent message() method
  • Missing semicolons (not in this case)
  • Incorrect catch syntax
5. You want to throw an exception only if a variable $age is less than 18. Which code correctly does this and catches the exception to print its message "Too young"?
hard
A. try { if ($age < 18) { throw new Exception("Too young"); } } catch (Exception $e) { echo $e->getMessage(); }
B. try { if ($age < 18) { throw new Exception("Too young"); } } catch (Exception $e) { echo "Too young"; }
C. try { if ($age < 18) { throw Exception("Too young"); } } catch (Exception $e) { echo $e->getMessage(); }
D. if ($age < 18) { throw new Exception("Too young"); } catch (Exception $e) { echo $e->getMessage(); }

Solution

  1. Step 1: Identify correct try-catch structure

    The exception must be thrown inside a try block to be caught properly. try { if ($age < 18) { throw new Exception("Too young"); } } catch (Exception $e) { echo $e->getMessage(); } correctly wraps the throw inside try.
  2. Step 2: Check exception throwing syntax and catch output

    try { if ($age < 18) { throw new Exception("Too young"); } } catch (Exception $e) { echo $e->getMessage(); } uses correct syntax throw new Exception("Too young") and prints the exception message with echo $e->getMessage();.
  3. Final Answer:

    try { if ($age < 18) { throw new Exception("Too young"); } } catch (Exception $e) { echo $e->getMessage(); } -> Option A
  4. Quick Check:

    Throw inside try, catch prints message [OK]
Hint: Always throw exceptions inside try blocks to catch them [OK]
Common Mistakes:
  • Throwing outside try block
  • Missing 'new' keyword when throwing
  • Printing hardcoded message instead of exception message
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard