When is error checking too much?

In the course of my PHP training, I tried to get acquainted with best practices for reporting and error handling, but the statements vary from person to person, and I struggled to develop a clear way to handle errors in my Application. I use exceptions to things that may go wrong, but for the most part it’s hard for me to understand if the exception should kill the application and display the error page, or just be caught and silent.

Something that seems to elude me, is there such a thing as too many messages? Every time you call a function, something can go horribly wrong, meaning that if you have to confirm every call to the function, you will have to fill in the pages with if statements and work out something that could affect one of the failures on the others. Is there a compressed document or idea for error messages that could clarify this for me? Are there any best practices? What are the best examples of good error handling?

I am currently doing the following:

  • Add important event results to an array that will be logged and emailed to me if a fatal error occurs.
  • Show abstract / general errors for fatal errors.
  • Use exceptions for cases that may fail
  • Enable error reporting in the development environment and disable for live environments.
  • Confirm all user input
  • Remediation of invalid user input
  • Display concise, informative error messages to users without providing a platform for operation.
+5
source share
1 answer

Exceptions - this is the only thing you do not understand IMHO: exceptions are designed to get out of your control, should be caught if they are considered from outside the limits into which they fell. The try block has a certain limit: it must contain related actions. For example, take a catch try block:

$array = array();
try {
    // connect throws exception on fail
    // query throws exception on fail
    // fetch results into $array
} catch (...) {
    $array[0]['default'] = 'me';
    $array[0]['default2'] = ...;
    ...
}

, try. , , . , , . - , $, : , , .

, :

$array = array();
try {
    if (!file_exists('file.php')) throw new Exception('file does not exists');
    include('file.php');
} catch (Exception $e) {
    trigger_error($e->getMessage());
}

. :

if (!file_exists('file.php')) trigger_error('file does not exists');
    include('file.php');
+3

All Articles