The PHP Exception::getCode API performs a focused operation on an object or value. The examples below show its effect on actual data.
Returns the numeric code attached to a Throwable; it is not necessarily an HTTP status code.
What Is Exception::getCode in PHP?
Returns the numeric code attached to a Throwable; it is not necessarily an HTTP status code.
Syntax:
Exception::getCode()
This operation has no parameters.
Return type declared by PHP: mixed / not explicitly declared. The value or side effect is demonstrated in the examples below.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$originalError = new Exception("Unable to process order", 17, new RuntimeException("Underlying failure"));
echo $originalError->getCode();
Output:
17
Returns the numeric code attached to a Throwable; it is not necessarily an HTTP status code.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$originalError = new Exception("Unable to read configuration", 42, new RuntimeException("Missing file"));
echo $originalError->getCode();
Output:
42
The line number and trace describe where the exception object was created or thrown; do not reveal full traces to public visitors.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$originalError = new Exception("Unexpected response", 503, new RuntimeException("Timeout"));
echo $originalError->getCode();
Output:
503
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- The line number and trace describe where the exception object was created or thrown; do not reveal full traces to public visitors.
- Returns the numeric code attached to a Throwable; it is not necessarily an HTTP status code.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
Exception::getCode | Returns the numeric code attached to a Throwable; it is not necessarily an HTTP status code. |
| Error | Exception is commonly thrown by applications, while Error represents PHP runtime issues; both expose Throwable diagnostic methods. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of Exception::getCode | The line number and trace describe where the exception object was created or thrown; do not reveal full traces to public visitors. |
| 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 | Exception is commonly thrown by applications, while Error represents PHP runtime issues; both expose Throwable diagnostic methods. |
Key Takeaways
- Returns the numeric code attached to a Throwable; it is not necessarily an HTTP status code.
- The line number and trace describe where the exception object was created or thrown; do not reveal full traces to public visitors.
- Use the example that matches the values, runtime version and dependencies in your application.
Exception::getCode 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.