Skip to content
Related Articles

Related Articles

Improve Article

PHP | Predefined Exceptions

  • Last Updated : 10 Jun, 2020

Predefined exceptions are available in PHP for more efficient exception handling but prerequisite to this article is learning exception handling  

Exception: This feature is available in PHP version 5 and 7. In version 7, it behaves a base class for every user-defined exception, but in version 5, it behaves a base class for every exception present.

There are 10 methods and 4 properties of the Exception class.

The four properties are –

Short description

data typeFull description
message string The message to be displayed when an exception occurs.
code int The code of the exception when an exception occurs.
file string The name of the file in which exception occurs.
line int The number of the line in which exception occurs.

The ten methods in Exception are

Function namenumber of argumentsname of argumentsreturn typedescription
__construct 3

message (string)

code (int)


voidan exception is constructed

returns the message to be displayed when exception occurs

getPrevious 0voidthrowtableprevious exceptions are returned
getCode 0voidmixedThe code for exception generated
getFile0voidstringThe file in which exception occurs in returned
getLine 0voidintThe number line in which exception occurs returned
getTrace0voidarrayThe stack trace is returned
getTraceAsString0voidstringThe stack trace is returned in the form of string
__toString0voidstringThe exception generated is returned in the form of string
__clone0voidvoidThe exception generated is cloned

ErrorException: The ErrorException class is just an extended version of the Exception class. It has only one new added property, rest have been inherited from the Exception class. There are two new additional methods, one for construction of exception and the second which returns the severity of the exception occurred.

The additional one properties are –

Short descriptionData typeFull description

for indicating severity of the exception occurred.

The additional to methods are –

Function namenumber of argumentsname of argument return typedescription
__construct6message (string)
code (int)
severity (int)
file name (string)
line number (int)
previous exception (throwtable)
voidan exception is constructed
getSeverity0voidintthe function returns severity of the exception

Error: All the internal errors getting generated in PHP are from this base class. It has similar features like Exception class. It has four properties and ten methods. The function name are the same and the properties are all the same. The difference in these two classes is that the Exception class methods return information about the exception and Error class returns information about internal errors in PHP.

ArgumentCountError: It comes into use when the expected number of arguments are not passed to a user-defined function as parameters. It is an extension of TypeError class where all the methods and properties have been inherited from it.

ArithmeticError: When arithmetic calculations are being performed in PHP and an exception occurs then this class is brought into use. In PHP version 7, when such type of error occurs negative value bit shifting is also attempted. This class is an extension of class Error and hence all the methods are properties that have just been inherited from the class Error.

AssertionError: If an assertion has been performed through the function assert and it results in an exception then this class comes into use. The function assert() is used to calculate assertion and this function returns a Boolean value. This function is a helpful feature when it comes to debugging.

DivisionByZeroError: If a number is being divided by zero, then this class is used which is an extension of class ArithmeticError.

CompileError: If errors occur during compilation then compilation errors occur which class CompileError class. Normally compilation errors are fatal errors. This class is an extension of class Error.

ParseError: When PHP code is being parsed and an exception occurs, this class comes into use. It is an extension of class CompileError. Function like eval() which is a language construct generates this type of exception.

TypeError: This class is an extension of class Error. There are three main reasons for the generation of such type of exception.

  • The parameter type declared in the function doesn’t match the type of the arguments being passed.
  • The return type of function that was declared doesn’t match the type which is being returned.
  • There are some built-in PHP functions which take restricted and certain type of parameters. If the wrong type or number of arguments is passes to them this exception occurs.
My Personal Notes arrow_drop_up
Recommended Articles
Page :