PHP Throwable interface


Introduction

In PHP 7, Throwable interface acts as base for any object that can be a parameter to throw statement, including Error and Exception. Both Error and Exception classes, from which predefined and user defined error and exception classes are derived respectively, implement Throwable interface. Following abstract methods are defined in Throwable interface −

Syntax

Throwable {
   /* Methods */
   abstract public getMessage ( void ) : string
   abstract public getCode ( void ) : int
   abstract public getFile ( void ) : string
   abstract public getLine ( void ) : int
   abstract public getTrace ( void ) : array
   abstract public getTraceAsString ( void ) : string
   abstract public getPrevious ( void ) : Throwable
   abstract public __toString ( void ) : string
}

Methods

getMessage ( void ) − string -> Returns the message associated with the thrown object.

getCode ( void ) − int -> Returns the error code associated with the thrown object.

getFile ( void ) − string -> Get the name of the file in which the thrown object was created.

getLine ( void ) − int -> Returns the line number where the thrown object was instantiated.

getTrace ( void ) − array -> Returns the stack trace as an array.

getTraceAsString ( void ) − string -> Returns the stack trace as a string.

getPrevious ( void ) − Throwable -> Returns any previous Throwable (provided as third parameter to Exception::__construct()).

__toString ( void ) − string -> Gets a string representation of the thrown object

Updated on: 21-Sep-2020

341 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements