Bird
Raised Fist0
PHPprogramming~20 mins

Try-catch execution flow in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Try-Catch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of try-catch with exception thrown
What is the output of this PHP code?
PHP
<?php
try {
    echo "Start\n";
    throw new Exception("Error happened");
    echo "End\n";
} catch (Exception $e) {
    echo "Caught: " . $e->getMessage() . "\n";
}
?>
A
Caught: Error happened
Start
B
Start
End
Caught: Error happened
C
Start
End
D
Start
Caught: Error happened
Attempts:
2 left
💡 Hint
Remember that code after throw inside try block is not executed.
❓ Predict Output
intermediate
2:00remaining
Output when no exception is thrown
What will this PHP code output?
PHP
<?php
try {
    echo "Before\n";
    // no exception thrown
    echo "After\n";
} catch (Exception $e) {
    echo "Caught\n";
}
?>
A
Before
After
B
Before
Caught
C
Caught
After
D
After
Attempts:
2 left
💡 Hint
If no exception is thrown, catch block is skipped.
❓ Predict Output
advanced
2:00remaining
Output with finally block execution
What is the output of this PHP code?
PHP
<?php
try {
    echo "Try block\n";
    throw new Exception("Oops");
} catch (Exception $e) {
    echo "Catch block\n";
} finally {
    echo "Finally block\n";
}
?>
A
Try block
Catch block
B
Try block
Finally block
Catch block
C
Try block
Catch block
Finally block
D
Catch block
Finally block
Attempts:
2 left
💡 Hint
Finally block always runs after try and catch.
❓ Predict Output
advanced
2:00remaining
Output when RuntimeException is thrown
What is the output of this PHP code?
PHP
<?php
try {
    echo "Start\n";
    throw new RuntimeException("Runtime error");
} catch (Exception $e) {
    echo "Caught Exception\n";
}
?>
A
Start
Caught Exception
B
Start
Fatal error: Uncaught RuntimeException
C
Caught Exception
D
Start
Attempts:
2 left
💡 Hint
RuntimeException extends Exception, so catch(Exception $e) catches it.
❓ Predict Output
expert
3:00remaining
Output with nested try-catch and rethrow
What is the output of this PHP code?
PHP
<?php
try {
    try {
        echo "Inner try\n";
        throw new Exception("Inner error");
    } catch (Exception $e) {
        echo "Inner catch\n";
        throw $e;
    } finally {
        echo "Inner finally\n";
    }
} catch (Exception $e) {
    echo "Outer catch\n";
} finally {
    echo "Outer finally\n";
}
?>
A
Inner try
Inner catch
Outer catch
Inner finally
Outer finally
B
Inner try
Inner catch
Inner finally
Outer catch
Outer finally
C
Inner try
Inner finally
Inner catch
Outer catch
Outer finally
D
Inner try
Inner catch
Inner finally
Outer finally
Attempts:
2 left
💡 Hint
Remember finally blocks run before control leaves the try or catch blocks.

Practice

(1/5)
1.

What is the main purpose of a try-catch block in PHP?

easy
A. To handle errors without stopping the program
B. To speed up the execution of the program
C. To create new variables automatically
D. To print output to the screen

Solution

  1. Step 1: Understand the role of try-catch

    The try block contains code that might cause an error, and the catch block handles that error.
  2. Step 2: Identify the purpose

    This structure prevents the program from stopping abruptly by managing errors gracefully.
  3. Final Answer:

    To handle errors without stopping the program -> Option A
  4. Quick Check:

    Try-catch = error handling [OK]
Hint: Try-catch is for error handling, not speed or output [OK]
Common Mistakes:
  • Thinking try-catch speeds up code
  • Confusing try-catch with output commands
  • Believing try-catch creates variables
2.

Which of the following is the correct syntax to start a try-catch block in PHP?

?
easy
A. try() { /* code */ } catch() { /* handle */ }
B. try: /* code */ catch: /* handle */
C. try { /* code */ } catch (Exception $e) { /* handle */ }
D. try[] { /* code */ } catch[] { /* handle */ }

Solution

  1. Step 1: Recall PHP try-catch syntax

    PHP uses curly braces {} to define blocks after try and catch keywords.
  2. Step 2: Match correct syntax

    try { /* code */ } catch (Exception $e) { /* handle */ } correctly uses try { } and catch (Exception $e) { } syntax.
  3. Final Answer:

    try { /* code */ } catch (Exception $e) { /* handle */ } -> Option C
  4. Quick Check:

    Curly braces with try and catch = correct syntax [OK]
Hint: Remember try and catch use curly braces in PHP [OK]
Common Mistakes:
  • Using colons instead of braces
  • Adding parentheses after try or catch
  • Using square brackets instead of braces
3.

What will be the output of this PHP code?

try {
    echo "Start\n";
    throw new Exception("Error happened");
    echo "End\n";
} catch (Exception $e) {
    echo "Caught: " . $e->getMessage() . "\n";
}
echo "Done\n";
medium
A. Start Caught: Error happened Done
B. Start Done
C. Caught: Error happened Done
D. Start End Caught: Error happened Done

Solution

  1. Step 1: Trace code inside try block

    "Start" is printed, then an Exception is thrown, so the next echo "End" is skipped.
  2. Step 2: Catch block runs and prints message

    The catch block prints "Caught: Error happened" and then after try-catch, "Done" is printed.
  3. Final Answer:

    Start Caught: Error happened Done -> Option A
  4. Quick Check:

    Exception stops try, catch prints message, then Done [OK]
Hint: Exception skips rest of try, catch runs, then code continues [OK]
Common Mistakes:
  • Thinking code after throw runs
  • Missing catch block output
  • Assuming program stops after exception
4.

Find the error in this PHP code snippet:

try {
    echo "Hello";
} catch {
    echo "Error caught";
}
medium
A. try block cannot have echo statements
B. Missing exception variable in catch parentheses
C. Missing semicolon after echo statements
D. catch block must come before try block

Solution

  1. Step 1: Check catch block syntax

    In PHP, catch must specify the exception type and variable, like catch (Exception $e).
  2. Step 2: Identify missing part

    The catch block here has no parentheses or variable, causing a syntax error.
  3. Final Answer:

    Missing exception variable in catch parentheses -> Option B
  4. Quick Check:

    Catch needs (Exception $e) [OK]
Hint: Catch always needs (Exception $e) or similar [OK]
Common Mistakes:
  • Omitting exception variable in catch
  • Thinking echo needs semicolon after last statement only
  • Believing catch can come before try
5.

Consider this PHP code:

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, 2) . "\n";
echo divide(5, 0) . "\n";

What will be the output?

hard
A. Error Error
B. 5 0
C. Division by zero Division by zero
D. 5 Division by zero

Solution

  1. Step 1: Analyze divide(10, 2)

    Since 2 is not zero, division happens: 10 / 2 = 5 is returned.
  2. Step 2: Analyze divide(5, 0)

    Since 0 triggers exception, catch returns the message "Division by zero".
  3. Final Answer:

    5 Division by zero -> Option D
  4. Quick Check:

    Valid division returns number; zero divisor returns error message [OK]
Hint: Exception returns message; normal runs return result [OK]
Common Mistakes:
  • Expecting zero instead of error message
  • Thinking exception stops program without catch
  • Confusing return values inside try-catch
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