Bird
Raised Fist0
PHPprogramming~20 mins

Custom exception classes in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of creating a custom exception class in PHP?

easy
A. To define specific error types for clearer error handling
B. To replace the built-in Exception class completely
C. To speed up the execution of the program
D. To automatically fix errors in the code

Solution

  1. Step 1: Understand the role of custom exceptions

    Custom exceptions allow programmers to create specific error types that represent particular problems in the code.
  2. Step 2: Compare with other options

    Custom exceptions do not replace the built-in Exception class, nor do they speed up execution or fix errors automatically.
  3. Final Answer:

    To define specific error types for clearer error handling -> Option A
  4. Quick Check:

    Custom exceptions = Specific error types [OK]
Hint: Custom exceptions clarify errors by type, not fix or speed [OK]
Common Mistakes:
  • Thinking custom exceptions replace built-in Exception
  • Believing custom exceptions fix errors automatically
  • Assuming custom exceptions improve performance
2.

Which of the following is the correct way to declare a custom exception class named MyError in PHP?

?
easy
A. class MyError extends Error {}
B. class MyError implements Exception {}
C. class MyError extends Exception {}
D. class MyError inherits Exception {}

Solution

  1. Step 1: Recall PHP syntax for custom exceptions

    Custom exceptions extend the built-in Exception class using the keyword extends.
  2. Step 2: Check each option

    class MyError extends Exception {} correctly uses extends Exception. class MyError implements Exception {} wrongly uses implements, which is for interfaces. class MyError extends Error {} extends Error, which is a different base class. class MyError inherits Exception {} uses invalid syntax inherits.
  3. Final Answer:

    class MyError extends Exception {} -> Option C
  4. Quick Check:

    Custom exception syntax = extends Exception [OK]
Hint: Use 'extends Exception' to create custom exceptions [OK]
Common Mistakes:
  • Using 'implements' instead of 'extends'
  • Using 'inherits' keyword which is invalid
  • Extending Error instead of Exception
3.

What will be the output of the following PHP code?

class MyException extends Exception {}

try {
    throw new MyException("Custom error happened");
} catch (MyException $e) {
    echo $e->getMessage();
} catch (Exception $e) {
    echo "General error";
}
medium
A. Custom error happened
B. General error
C. Fatal error: Uncaught exception
D. No output

Solution

  1. Step 1: Understand the thrown exception type

    The code throws an exception of type MyException.
  2. Step 2: Check catch blocks order and matching

    The first catch block matches MyException, so it catches the exception and echoes the message.
  3. Final Answer:

    Custom error happened -> Option A
  4. Quick Check:

    Matching catch block runs = Custom message [OK]
Hint: Catch specific exceptions before general ones [OK]
Common Mistakes:
  • Expecting general catch to run first
  • Thinking exception is uncaught
  • Ignoring exception message
4.

Identify the error in this PHP code that defines and uses a custom exception:

class MyException extends Exception {}

try {
    throw new MyException("Error!");
} catch (Exception $e) {
    echo $e->getMessage();
} catch (MyException $e) {
    echo "Caught MyException";
}
medium
A. No error; code runs fine
B. The catch blocks are in wrong order; specific should come before general
C. The throw statement syntax is incorrect
D. The class should implement Exception interface

Solution

  1. Step 1: Analyze catch block order

    The first catch block catches all Exception types, including MyException, so the second catch block is unreachable.
  2. Step 2: Correct catch order

    Specific exceptions like MyException must be caught before the general Exception to avoid unreachable code.
  3. Final Answer:

    The catch blocks are in wrong order; specific should come before general -> Option B
  4. Quick Check:

    Specific catch before general catch [OK]
Hint: Catch specific exceptions before general ones to avoid unreachable code [OK]
Common Mistakes:
  • Placing general catch before specific catch
  • Thinking implements is needed for exceptions
  • Misunderstanding throw syntax
5.

Given this custom exception class and code, what will be the output?

class ValidationException extends Exception {
    public function getCustomMessage() {
        return "Validation failed: " . $this->getMessage();
    }
}

try {
    throw new ValidationException("Invalid input");
} catch (ValidationException $e) {
    echo $e->getCustomMessage();
} catch (Exception $e) {
    echo "General error";
}
hard
A. Fatal error: Call to undefined method
B. Invalid input
C. General error
D. Validation failed: Invalid input

Solution

  1. Step 1: Understand the custom method in exception

    The class ValidationException adds a method getCustomMessage() that returns a formatted message.
  2. Step 2: Check which catch block runs and output

    The thrown exception is ValidationException, so the first catch block runs and echoes the custom message.
  3. Final Answer:

    Validation failed: Invalid input -> Option D
  4. Quick Check:

    Custom method called on caught exception [OK]
Hint: Call custom methods on caught custom exceptions [OK]
Common Mistakes:
  • Expecting default getMessage() output only
  • Thinking general catch block runs first
  • Assuming method does not exist
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