Challenge - 5 Problems
Custom Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of custom exception message
What is the output of this PHP code when the exception is thrown and caught?
PHP
<?php class MyException extends Exception {} try { throw new MyException("Error happened"); } catch (MyException $e) { echo $e->getMessage(); } ?>
Attempts:
2 left
💡 Hint
Look at how the exception message is accessed inside the catch block.
✗ Incorrect
The custom exception inherits from Exception and the message passed is 'Error happened'. The catch block prints the message using getMessage(), so the output is exactly that string.
❓ Predict Output
intermediate2:00remaining
Custom exception with additional property
What will be the output of this PHP code?
PHP
<?php class MyException extends Exception { private $codeNumber; public function __construct($message, $codeNumber) { parent::__construct($message); $this->codeNumber = $codeNumber; } public function getCodeNumber() { return $this->codeNumber; } } try { throw new MyException("Error", 404); } catch (MyException $e) { echo $e->getMessage() . ' ' . $e->getCodeNumber(); } ?>
Attempts:
2 left
💡 Hint
Check how the custom property is set and accessed.
✗ Incorrect
The constructor sets the message and the custom codeNumber property. The catch block prints both the message and the codeNumber, so output is 'Error 404'.
❓ Predict Output
advanced2:00remaining
Exception inheritance and catch order
What will be the output of this PHP code?
PHP
<?php class BaseException extends Exception {} class ChildException extends BaseException {} try { throw new ChildException("Child error"); } catch (BaseException $e) { echo "Caught BaseException: " . $e->getMessage(); } catch (ChildException $e) { echo "Caught ChildException: " . $e->getMessage(); } ?>
Attempts:
2 left
💡 Hint
Remember catch blocks are checked in order and ChildException is a subclass of BaseException.
✗ Incorrect
The first catch block matches because ChildException is a subclass of BaseException. So it catches the exception and prints the message with 'Caught BaseException:'. The second catch block is never reached.
❓ Predict Output
advanced2:00remaining
Custom exception with overridden __toString method
What is the output of this PHP code?
PHP
<?php class MyException extends Exception { public function __toString() { return "MyException: " . $this->getMessage(); } } try { throw new MyException("Something went wrong"); } catch (MyException $e) { echo $e; } ?>
Attempts:
2 left
💡 Hint
Check how the __toString method affects printing the exception object.
✗ Incorrect
The __toString method is overridden to return 'MyException: ' plus the message. When echoing the exception object, this method is called, so the output is 'MyException: Something went wrong'.
🧠 Conceptual
expert2:00remaining
Behavior of uncaught custom exceptions
What happens if a custom exception thrown in PHP is not caught anywhere in the code?
Attempts:
2 left
💡 Hint
Think about what happens when an exception is not handled in PHP.
✗ Incorrect
In PHP, if an exception is thrown and not caught, the script stops execution and a fatal error message is shown with the exception details. This helps to avoid silent failures.