reference appendices
Predefined Exceptions
PHP and SPL provide exception classes for common failure categories. Choose a type that communicates the failure, and catch exceptions only where the application can add context, recover, or translate the error.
Use This Reference When
- Throwing
InvalidArgumentExceptionfor invalid caller input. - Recognising runtime, logic, range, length, and out-of-bounds failures.
- Reviewing whether a broad
catch (Throwable)belongs at an application boundary.
Report an Invalid Argument
PHP example
<?php
function requirePositive(int $quantity): int
{
if ($quantity < 1) {
throw new InvalidArgumentException('Quantity must be positive.');
}
return $quantity;
}
echo requirePositive(2) . PHP_EOL;
// Prints:
// 2
Application-specific exception classes are still useful when callers need a domain-specific recovery path.
Practice
Classify a Failure
Choose exception types for an invalid function argument, an unavailable configuration file, and a missing domain record. Explain where custom exceptions improve the API.
Show solution
Use a standard argument exception for caller misuse. File and domain failures may deserve custom exceptions when callers need to distinguish and handle them.