Vinod Sebastian – B.Tech, M.Com, PGCBM, PGCPM, PGDBIO

Hi I'm a Web Architect by Profession and an Artist by nature. I love empowering People, aligning to Processes and delivering Projects.

Advertisements

Errors and Exceptions in PHP

Error vs. Exception

In PHP, errors and exceptions play distinct roles in the handling of issues that may arise during script execution. Errors are predefined problems that occur when the script runs, while exceptions are objects specifically designed to represent and manage errors.

Types of Errors

  • Compile Time Errors: These errors are identified during the compilation of the code. They cannot be caught and will lead to the termination of the script if not rectified promptly.
  • Fatal Errors: Critical errors that abruptly halt the execution of the script and cannot be recovered from. These errors usually indicate severe issues in the code.
  • Recoverable Errors: Errors that can be managed within the script by implementing appropriate error-handling mechanisms. These errors allow for recovery and continuation of script execution.
  • Warnings: Warnings signal potential problems in the script but do not halt its execution. They serve as alerts for developers to investigate and address potential issues.
  • Notices: Notices provide informational messages about non-critical problems in the script without affecting its execution. These messages are typically used for debugging and optimization purposes.

Error Reporting in PHP

Controlling how errors are handled and displayed is crucial in PHP development. The error reporting directive in PHP allows developers to manage error visibility and logging effectively. The settings display_errors and log_errors determine whether errors are displayed on the screen and logged to a file, respectively.

To suppress errors for a specific expression, developers can utilize the @ symbol, known as the error control operator.

For custom error handling, developers can define a custom error handler using set_error_handler("functionName") to treat errors as exceptions and implement personalized error management logic.

set_error_handler("customError");
function customError($errno, $errstr) {
    // Custom error handling logic
}

Exceptions in PHP

  • Exception Class: In PHP, the Exception class serves as the foundation for exception handling. Developers have the flexibility to create custom exception classes by extending the base Exception class to address specific error scenarios.
  • Throwing Exceptions: To trigger an exception within a script, developers use the throw keyword to raise the exception and interrupt the normal flow of execution.
  • Exception Handling: PHP offers the capability to establish a catch-all function using set_exception_handler() to manage uncaught exceptions gracefully and provide a centralized approach to exception handling.
function handleUncaughtException($e) {
    echo $e->getMessage();
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x