Menu
Coddy logo textTech

Creating a Custom Exception

Part of the Logic & Flow section of Coddy's PHP journey — lesson 56 of 68.

While PHP's built-in Exception class works well for general error handling, creating your own custom exception classes provides much more specific and meaningful error messages. Custom exceptions help you distinguish between different types of problems in your code and handle them appropriately.

To create a custom exception, you extend PHP's base Exception class using the extends keyword. This inheritance gives your custom exception all the functionality of the standard exception while allowing you to add your own specific behavior or simply provide a more descriptive name.

<?php
class InvalidEmailException extends Exception {
    // The class can be empty - it inherits everything from Exception
}

// You can also add custom properties or methods
class CustomException extends Exception {
    public function getCustomMessage() {
        return "Custom error: " . $this->getMessage();
    }
}
?>

The power of custom exceptions lies in their descriptive names and the ability to catch specific exception types. Instead of catching a generic Exception, you can catch InvalidEmailException, DatabaseConnectionException, or any other custom exception you create. This makes your error handling more precise and your code more maintainable.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You will receive one input: a string representing an exception class name. This input will be a single word that should be used as the name for your custom exception class (example: NegativeBalanceException or InvalidUsernameException).

Read the input and create a custom exception class using the provided name. Your custom exception class should extend PHP's base Exception class. After creating the class, print "Custom exception created: " followed by the class name you received as input.

Input format: One line containing a string representing the exception class name (example: InvalidAgeException)

Expected output: Custom exception created: followed by the exception class name

Cheat sheet

To create custom exceptions in PHP, extend the base Exception class using the extends keyword:

<?php
class InvalidEmailException extends Exception {
    // The class can be empty - it inherits everything from Exception
}

// You can also add custom properties or methods
class CustomException extends Exception {
    public function getCustomMessage() {
        return "Custom error: " . $this->getMessage();
    }
}
?>

Custom exceptions provide more specific and meaningful error messages, allowing you to catch specific exception types instead of generic Exception objects. This makes error handling more precise and code more maintainable.

Try it yourself

<?php
// Read the exception class name
$className = trim(fgets(STDIN));

// TODO: Write your code below to create the custom exception class


// Output the result
echo "Custom exception created: " . $className;
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow