0
0
PHPprogramming~20 mins

Custom exception classes in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
}
?>
ANo output
BFatal error
CError happened
DException
Attempts:
2 left
💡 Hint
Look at how the exception message is accessed inside the catch block.
Predict Output
intermediate
2: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();
}
?>
AError 404
BError 0
CError
DFatal error: Uncaught Error
Attempts:
2 left
💡 Hint
Check how the custom property is set and accessed.
Predict Output
advanced
2: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();
}
?>
ANo output
BCaught ChildException: Child error
CFatal error: Uncaught ChildException
DCaught BaseException: Child error
Attempts:
2 left
💡 Hint
Remember catch blocks are checked in order and ChildException is a subclass of BaseException.
Predict Output
advanced
2: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;
}
?>
AMyException: Something went wrong
BSomething went wrong
CFatal error: Object of class MyException could not be converted to string
DException: Something went wrong
Attempts:
2 left
💡 Hint
Check how the __toString method affects printing the exception object.
🧠 Conceptual
expert
2:00remaining
Behavior of uncaught custom exceptions
What happens if a custom exception thrown in PHP is not caught anywhere in the code?
AThe exception is ignored and the script continues running normally.
BThe script stops and a fatal error message with the exception details is displayed.
CThe exception is automatically caught by a default handler and logged silently without stopping the script.
DThe exception causes a warning but the script continues execution.
Attempts:
2 left
💡 Hint
Think about what happens when an exception is not handled in PHP.