
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Extending Exceptions
Introduction
Exception class implements Throwable interface and is base class for all Exception classes, predefined exceptions as well as user defined exceptions. The Exception class defines some final (non-overridable) methods to implement those from Throwable interface, and __tostring() method that can be overridden to return a string representation of Exception object.
final public function getMessage() | message of exception |
final public function getCode() | code of exception |
final public function getFile() | source filename |
final public function getLine() | source line |
final public function getTrace() | an array of the backtrace() |
final public function getPrevious() | previous exception |
final public function getTraceAsString() | formatted string of trace |
public function __toString() | formatted string for display |
If user defined exception class re-defines the constructor, it should call parent::__construct() to ensure all available data has been properly assigned.
Example
Following script defines a custom exception class called myException. This type of exception is thrown if value of $num is less than 0 or greater than 100. The getMessage() method of Exception class returns the error message and getLine() method returns line of code in which exception appears
Example
<?php class myException extends Exception{ function message(){ return "error : " . $this->getMessage() . " in line no " . $this->getLine(); } } $num=125; try{ if ($num>100 || $num<0) throw new myException("$num is invalid number"); else echo "$num is a valid number"; } catch (myException $m){ echo $m->message(); } ?>
Output
Run above code with $num=125 and $num=90 to get error message and message of valid number
error : 125 is invalid number in line no 10
- Related Articles
- PHP Throwing Exceptions
- Exceptions and Error in PHP 7
- Extending namespace and Unnamed namespace
- Extending User Interface with CSS3
- Extending SAP ABAP 30 characters long limit
- Extending a list in Python (5 different ways)
- Concrete Exceptions in Python
- Custom exceptions in Java
- Chained Exceptions in C#
- Throwing exceptions from C++ constructors
- Raising an Exceptions in Python
- User-Defined Exceptions in Python
- Are Python Exceptions runtime errors?
- Built-in Exceptions in C#
- How to define custom JavaScript exceptions?
