Custom exception classes represent specific application failures that callers can catch individually.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Custom Exception Classes Works
Custom exception classes represent specific application failures that callers can catch individually.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
class InvalidQuantity extends DomainException {}
try { throw new InvalidQuantity("Quantity must be positive"); } catch (InvalidQuantity $error) { echo $error->getMessage(); }
Output:
Quantity must be positive
Custom exception classes represent specific application failures that callers can catch individually.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class ConfigurationFailure extends RuntimeException {}
try { throw new ConfigurationFailure("Missing setting"); } catch (ConfigurationFailure $error) { echo get_class($error); }
Output:
ConfigurationFailure
Extend an appropriate Throwable subtype and preserve original causes when wrapping low-level exceptions.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
try { try { throw new RuntimeException("Storage offline"); } catch (RuntimeException $error) { throw new DomainException("Order could not be saved", 0, $error); } } catch (DomainException $error) { echo $error->getPrevious()?->getMessage(); }
Output:
Storage offline
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Extend an appropriate Throwable subtype and preserve original causes when wrapping low-level exceptions.
- Custom exception classes represent specific application failures that callers can catch individually.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Custom Exception Classes | Custom exception classes represent specific application failures that callers can catch individually. |
| Alternative approach | Generic RuntimeException is sufficient for simple errors; custom types help when callers need separate handling. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Custom Exception Classes | Extend an appropriate Throwable subtype and preserve original causes when wrapping low-level exceptions. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Generic RuntimeException is sufficient for simple errors; custom types help when callers need separate handling. |
Key Takeaways
- Custom exception classes represent specific application failures that callers can catch individually.
- Extend an appropriate Throwable subtype and preserve original causes when wrapping low-level exceptions.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Custom Exception Classes is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.