In this chapter
We'll learn why the booking page showed a completely white page, the two kinds of problems PHP reports — classic errors like warnings, and Throwables that stop the code — how Exception and Error split the Throwable family, what every exception object carries, and how PHP's approach compares with Java, Python, JavaScript, Go and Rust.
The Problem in Real Life
Ten days before launch, Olivia clicks an old confirmation link from a test email: booking.php?id=4812. The page is completely white. No message, no logo, nothing — and the browser's network tab says "500 Internal Server Error".
Mia opens the PHP log. It's empty. The test server uses production settings, so display_errors is off (Act 01) — and so is log_errors, so PHP didn't write the error anywhere. Marcus turns logging on and loads the page again. One line appears: "PHP Fatal error: Uncaught Error: Call to a member function total() on null in booking.php:9". Booking 4812 was deleted with the test data, find() returned null, and the page called a method on it.
PHP knew exactly what went wrong. It just didn't tell anyone.
Mia
Silent Failure vs. Errors You Can See and Handle
Two kinds of problems
Warnings report a problem and carry on. Throwables stop the code unless something catches them.
One family tree
Everything that can be thrown is a Throwable: either an Exception or an Error.
Objects full of clues
An exception carries its message, the file and line, and the path of calls that led there.
Errors vs Exceptions: Throwable and the Hierarchy
What PHP does when something goes wrong: every program meets problems — missing data, wrong input, a database that's down, a bug. This Act is about noticing them, handling them and reporting them properly. This chapter lays the foundations:
- Two kinds of problems: classic errors (warnings, notices, deprecations) and Throwables.
- Exceptions: objects that are thrown, travel up and are caught.
- Throwable, Exception and Error: the family tree.
- What an exception carries: message, code, file, line and trace.
- Other languages: checked exceptions, error values and Result types.
Two kinds of problems: PHP reports problems in two different ways. Classic errors are messages: a warning ("Attempt to read property "customer" on null"), a notice or a deprecation is reported, and then the script carries on with the next line, usually with a null or false where a real value should be. A Throwable is different: it stops the normal flow at once. Unless some code catches it, the script ends with a fatal error — "Uncaught Error: Call to a member function total() on null". The example below shows both from the same null booking: the warning lets the script continue, the Error ends it. That's what happened to booking 4812, and because the error happened before any HTML was printed, the page stayed white.
<?php$booking = null; // find(4812) found nothingecho $booking->customer, "|\n"; // a warning, then the script carries onecho $booking->total(); // an Error: the script ends here// Warning: Attempt to read property "customer" on null in main.php on line 3// |// Fatal error: Uncaught Error: Call to a member function total() on null in main.php:4
Exceptions: an exception is an object that describes a problem. Code throws it (throw new RuntimeException("Booking 4812 not found")), and from that moment PHP stops running the current function and looks for code that wants to catch it — first in the current function, then in the function that called it, and so on up the chain. Code that catches it can show a friendly message, try something else or log it. If nothing catches it, PHP prints or logs "Uncaught …" and ends the script. Chapter 2 covers throwing and catching; this chapter is about what gets thrown.
Throwable, Exception and Error: everything that can be thrown implements the interface Throwable. Your own classes can't implement it directly ("Class MyThrowable cannot implement interface Throwable, extend Exception or Error instead") — they extend one of its two branches. Exception is for problems a working program must expect: a booking that doesn't exist, a slot that's taken, a database that's down. Your own exceptions extend it (chapter 3). Error is what PHP itself throws for problems in the code: a wrong type (TypeError), a missing argument (ArgumentCountError), dividing by zero (DivisionByZeroError), calling a method on null. An Error almost always means a bug to fix, not a situation to handle. The diagram below shows the main branches; chapters 3 and 4 fill in the rest.
The Throwable family (main branches)
Throwable
interface: can be thrown and caught — never implemented directly
Exception
situations a working program must expect
Error
PHP's own errors: bugs in the code
RuntimeException, LogicException
and their children (chapter 3)
JsonException, ErrorException
built-in exceptions
TypeError → ArgumentCountError
wrong types, missing arguments
ValueError, ArithmeticError, …
chapter 4
SlotTakenException, DatabaseUnavailableException
your own classes extend Exception or its children
<?phptry {try {intdiv(1, 0); // PHP throws an Error} catch (Exception $e) {echo "never printed: an Error is not an Exception\n";}} catch (Error $e) {echo get_class($e), " is an Error\n"; // DivisionByZeroError is an Error}echo implode(" <- ", [JsonException::class, ...class_parents(JsonException::class)]), "\n";echo implode(" <- ", [ArgumentCountError::class, ...class_parents(ArgumentCountError::class)]), "\n";// JsonException <- Exception// ArgumentCountError <- TypeError <- Error
A short history: before PHP 7, "Call to a member function on null" was a real fatal error: nothing could catch it, and the script just died. PHP 7 turned most fatal errors into Error objects, which is why Error and Exception are separate: old code that caught every Exception shouldn't suddenly start catching programming bugs too. PHP 8 went further and turned many warnings from built-in functions into TypeError and ValueError — strlen([]) used to warn and return null, and now it throws. A few things are still real fatal errors that no code can catch, like running out of memory; chapter 4 lists them.
What an exception carries: every Throwable has the same methods. getMessage() is the text, getCode() a number (often 0), getFile() and getLine() say where it was created, and getTrace() / getTraceAsString() give the stack trace: the chain of function calls that led there, newest first. getPrevious() returns the exception that caused this one (chapter 3). Echoing an exception prints all of it at once. The trace is the most useful debugging tool there is: "load(4812) was called by show(4812), which was called by the main script" tells you the whole story of the failure.
<?phpfunction load(int $id){throw new RuntimeException("Booking $id not found", 404);}function show(int $id){return load($id);}try {show(4812);} catch (RuntimeException $e) {echo $e->getMessage(), " | code ", $e->getCode(), " | line ", $e->getLine(), "\n";echo $e->getTraceAsString(), "\n";}// Booking 4812 not found | code 404 | line 4// #0 main.php(9): load(4812)// #1 main.php(13): show(4812)// #2 {main}
Other languages: Java has the same split — Throwable with Exception and Error — and adds checked exceptions: a method must declare throws SQLException, and callers must catch it or declare it too, or the program won't compile. PHP doesn't support checked exceptions; any function can throw anything, and teams document it with @throws comments that PHPStan (Act 21) can check. Python has BaseException → Exception, and JavaScript can throw any value at all — even a string — while PHP only throws Throwable objects. Go and Rust have no exceptions for normal failures: a function returns an error value (Go) or a Result (Rust), and the caller must deal with it. PHP has no Result type built in; chapter 7 shows when returning null is better than throwing. The table below compares them.
| Language | Mechanism | Must the caller handle it? | Can any value be thrown? |
|---|---|---|---|
| PHP | Exceptions (Throwable: Exception / Error) | No — @throws is only documentation | No: only Throwable objects |
| Java | Exceptions (Throwable: Exception / Error) | Yes for checked exceptions | No: only Throwable objects |
| Python | Exceptions (BaseException → Exception) | No | No: only exception objects |
| JavaScript | Exceptions | No | Yes: even strings and numbers |
| Go | Returned error values | By convention (if err != nil) | No exceptions for normal failures |
| Rust | Returned Result<T, E> | Yes, the compiler insists | No exceptions |
// Java — the compiler insists that callers deal with SQLExceptionpublic Booking find(int id) throws SQLException {return repository.load(id);}
// Go — no exception: the caller checks the error valuebooking, err := repository.Find(4812)if err != nil {return fmt.Errorf("loading booking: %w", err)}
Key Takeaway
PHP reports problems two ways: classic errors (warnings, notices, deprecations) are messages after which the script carries on, while Throwables stop the code unless caught — uncaught, they end the script with a fatal error. Everything throwable implements Throwable through one of two branches: Exception, for situations a working program must expect (your own exceptions extend it), and Error, which PHP throws for bugs like TypeError or calling a method on null. Every Throwable carries a message, code, file, line, previous exception and stack trace. Unlike Java, PHP has no checked exceptions; unlike Go and Rust, it reports failures by throwing rather than returning error values.
Why This Matters
Every PHP application meets failures every day: missing records, bad input, slow services. Knowing which ones PHP reports as warnings and which ones it throws decides whether your code notices them at all.
Exceptions are also how frameworks talk to you: Laravel throws ModelNotFoundException, ValidationException and dozens more, and turns them into 404 pages, error messages and log entries. Understanding the hierarchy is understanding that whole system.
Mia now knows what killed the page: an Error, thrown because find() returned null, and caught by nobody. Her first instinct is to wrap the whole page in a try block. But what should it catch, what should the user see, and what should still run afterwards? That's the next chapter: throwing, catching and rethrowing.
