How is error handling done in php?

Prerequisite: Types of Error 
PHP is used for web development. Error handling in PHP is almost similar to error handling in all programming languages. The default error handling in PHP will give file name line number and error type.
 

Ways to handle PHP Errors:  

  • Using die[] method
  • Custom Error Handling

Basic error handling: Using die[] function The die[] function print a message and exit from current script.
Syntax:  

die[ $message ]

Example:  

php

Note: Run the above code and geeks.txt file is not present then it will display an run-time error message. 
Runtime Error: 

 PHP Warning: fopen[geeks.txt]: failed to open stream: Permission denied 
in /home/dac923dff0a2558b37ba742613273073.php on line 2

To prevent this error use die[] function. Below is the implementation of die[] function:
Example:  

php

Note: If geeks.txt file not present then it will display output. 
Output 

File is not present

Custom Error handling: Creating a custom error handler in PHP is quite simple. Create a function that can be called when a error has been occurred in PHP.
Syntax:  

error_function[ $error_level, $error_message, $error_file, $error_line, $error_context]

Parameters: This function accepts five parameters as mentioned above and described below:  

  • $error_level: It is required parameter and it must be an integer. There are predefined error levels.
  • $error_message: It is required parameter and it is the message which user want to print.
  • $error_file: It is optional parameter and used to specify the file in which error has been occurred.
  • $error_line: It is optional parameter and used to specify the line number in which error has been occurred.
  • $error_context: It is optional parameter and used to specify an array containing every variable and their value when error has been occurred.

error_level: These are the possible error level which are listed below: 

  • 1 : .E_ERROR :fatal runtime error execution of script has been halted
  • 2 : E_WARNING :non fatal runtime error execution of script has been halted
  • 4 : E_PARSE :compile time error it is generated by the parser
  • 8 :E_NOTICE :The script found something that might be an error
  • 16 :E_CORE_ERROR :Fatal errors that occurred during initial startup of script
  • 32 :E_CORE_WARNING :Non fatal errors that occurred during initial startup of script
  • 8191 :E_ALL :All errors and warning

set_error_handler[] Function: After creating myerror[] function need to set custom error handler because in normal way PHP handles it but if user doing custom error handling then user have to set it in place of argument and pass out myerror function as a string.
Example: 

php

Output: 

Error: [2] Division by zero 
 Now Script will end

Conclusion: It is always try to error handling using Custom error handling because it will show more specified message according to the user that can be helpful to the user. If error is not handle using Custom error handling then a error occurred then out script will be halted by default but if it handle error using Custom error handling then it can continue script after displaying error message.


How error handling is done?

When it comes to error handling in software, either the programmer develops the necessary codes to handle errors or makes use of software tools to handle the errors. In cases where errors cannot be classified, error handling is usually done with returning special error codes.

What are PHP error handling keywords?

PHP error handling keywords Throw: The throw keyword is used to signal the occurrence of a PHP exception. The PHP runtime will then try to find a catch statement to handle the exception. Catch: This block of code will be called only if an exception occurs within the try code block.

How database errors are handled using exception in PHP?

With PHP 5 came a new object oriented way of dealing with errors. Exception handling is used to change the normal flow of the code execution if a specified error [exceptional] condition occurs. This condition is called an exception.

Chủ Đề