PHP Exception Methods

Beginner
3 min read
Updated: Nov 2026
10 method guides
Method index

About This Index

This page is the PHP Exception Methods index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.

Learn how to throw, catch, chain, and inspect Exception objects on /php/exception. Use this page when you need a specific method such as getMessage(), getPrevious(), or getTraceAsString().

Beginner path.

Start with __construct() and getMessage()—then learn getPrevious() and getTraceAsString() for chaining and logs.

How to Call an Exception Method

Exception helpers are object methods—call them on a caught Exception instance:

PHP
try {
    throw new Exception("Not found", 404);
} catch (Exception $e) {
    echo $e->getMessage();
    echo $e->getCode();
    echo $e->getTraceAsString();
}

PHP Exception Methods Table

All method guides in this PHP tutorial series (10 tutorials in 4 categories).

Create

1 method

Build an Exception with a message, code, and optional previous cause.

MethodDescriptionTutorial
__construct()Construct the exception with message, code, and previous.Try it

Inspect basics

4 methods

Read the message, code, file, and line after you catch an exception.

MethodDescriptionTutorial
getMessage()Get the exception message string.Try it
getCode()Get the integer exception code.Try it
getFile()Get the file where the exception was created.Try it
getLine()Get the line where the exception was created.Try it

Chain & trace

4 methods

Follow nested causes and inspect the call stack.

MethodDescriptionTutorial
getPrevious()Return the previous Throwable, or null.Try it
getTrace()Get the stack trace as an array.Try it
getTraceAsString()Get the stack trace as a string.Try it
__toString()Return the string representation of the exception.Try it

Special

1 method

Understand the private final method that blocks cloning.

MethodDescriptionTutorial
__clone()Prevent cloning of exception objects.Try it

Frequently Asked Questions

They are the methods on the Exception class: create an exception, read its message and code, inspect file and line, walk previous causes, and print or inspect the stack trace. __clone() exists only to block cloning.
The Exception page teaches the ideas: throw, catch, chaining, and when to use each getter. This index lists every method tutorial so you can open getMessage(), getTraceAsString(), and the rest.
Start with __construct() and getMessage(). Then learn getCode(), getFile(), and getLine(). Add getPrevious() when you wrap errors, and getTraceAsString() when you need logs.
Beginners often try clone $e. The __clone() page explains that Exception objects are not cloneable and shows safer alternatives such as rethrowing or wrapping.
They are methods on Exception objects. You call them with $e->getMessage(), not as global functions. The left nav still groups them under the Exception function index for the tutorial track.
If you are new, read PHP Exception first. Then open __construct(), getMessage(), and getTraceAsString() from the tables below.

Start with __construct()

Open the first guide—syntax, examples, and FAQs.

__construct() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful