Skip to content
Learn Netverks

Lesson

Step 28/36 78% through track

error-handling

Errors, exceptions, and logging

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Errors, exceptions, and logging: the syntax, APIs, and habits you need before advancing in PHP.

Teams ship Errors, exceptions, and logging on every PHP codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Errors, exceptions, and logging in contexts like: LAMP/LEMP stacks, Laravel apps, WordPress themes/plugins, and shared hosting.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

When you can explain the previous lesson's ideas without copying starter code.

PHP reports notices, warnings, and errors. Modern code throws exceptions for exceptional paths and logs details instead of displaying them to users in production.

Exception basics

try {
    if ($id < 1) {
        throw new InvalidArgumentException('Invalid id');
    }
} catch (InvalidArgumentException $e) {
    http_response_code(400);
    echo json_encode(['error' => $e->getMessage()]);
}

Custom exceptions

Extend Exception or SPL types (RuntimeException, LogicException) for domain errors—catch specific types, not everything blindly.

Production vs development

  • Dev: display_errors=On, verbose stack traces
  • Prod: display_errors=Off, log to file/monitoring, generic user message

Important interview questions and answers

  1. Q: Error vs exception in PHP?
    A: Traditional errors are procedural; exceptions are OOP control flow—many errors convert to Error exceptions in PHP 7+ for fatals in some cases.
  2. Q: finally block?
    A: Runs whether or not catch matched—use for cleanup (close handles).

Self-check

  1. Should production show stack traces to visitors?
  2. When throw vs return an error array?

Challenge

Validate and throw

  1. Write a function that throws if email is invalid.
  2. Call it in try/catch and echo the message.

Done when: invalid email produces a caught exception message in output.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • try catch when?
  • Production display_errors?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump