Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | Predefined Exceptions

PHP | Predefined Exceptions

Predefined exceptions are available in PHP for more efficient exception handling but prerequisite to this article is learning exception handling https://www.geeksforgeeks.org/exception-handling-in-php/  

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 type Full 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 name number of arguments name of arguments return type description
__construct  3

message (string)

code (int)

previous(throwtable)

void an exception is constructed
getMessage 0 void string

returns the message to be displayed when exception occurs

getPrevious  0 void throwtable previous exceptions are returned
getCode  0 void mixed The code for exception generated
getFile 0 void string The file in which exception occurs in returned
getLine  0 void int The number line in which exception occurs returned
getTrace 0 void array The stack trace is returned
getTraceAsString 0 void string The stack trace is returned in the form of string
__toString 0 void string The exception generated is returned in the form of string
__clone 0 void void The 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 description Data type Full description
severity int

for indicating severity of the exception occurred.

The additional to methods are –

Function name number of arguments name of argument  return type description
__construct 6 message (string)
code (int)
severity (int)
file name (string)
line number (int)
previous exception (throwtable)
void an exception is constructed
getSeverity 0 void int the 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.

RELATED ARTICLES

Most Popular

Recent Comments