Author - StudySection Post Views - 196 views
MVC Exceptions

Handling exceptions in MVC application

Once you have handled all the exceptions using try-catch blocks, we need to decide what needs to be done when an exception is thrown. Logging is one of the best ways to handle these types of errors because one has a trace of what happened in any given section of your code and the error that was thrown. Logging tells you exactly where the bug is located in your code. We still might need to ask the users what they were doing or what input they sent, but most logs can help us identify bugs before users ever detect them.

If you log the bug and then gracefully send users to another page (such as redirecting to an error page), users will always be unaware that an exception was ever thrown.

Structure of log in PHP (Always follow the following structure while writing any log)


try{
}
catch ( Exception $e ) {
$resultData = print_r ( $e, 1 ); //exception information
$this->writeLog ( ‘AbcController:: xyz function::after calling “exception function name”:: Exception::’ . $resultData );
}
You can also log the date and time of the exception
try{
}
catch ( Exception $e ) {
$this->writeLog ( ‘AbcController:: xyz function:: “exception function name”:: response at time :’. date ( "d-m-Y h:i:sa" ) . " is \r\n" . $e );
}

Benefits of logging an exception:

  1. Developers can trace the issue easily. For eg. Sometimes there may be a case when errors are faced by one user and we can’t access that user’s account on our application. In this case, it will be very time consuming to find the issue by looking into the code, therefore, if an exception has been logged properly then the user can trace the issue easily.
  2. If the exception has been logged properly then it will be easy to fix the issue because the exception will let us know which controller and which method or you can say it will tell us the part of code that caused this exception.

Always take care of following things before logging :

  1. Your logs should contain descriptive information that helps you identify the cause of an error. Don’t just log “error” as it will give no idea about the error. Firstly, Log “Critical error in logging class” and then try logging the exception information.
  2. Don’t have an empty catch statement. You should log the information and then either redirect the user or show an appropriate error message.
  3. Always inherit from the Exception class for custom exceptions. We can define our own custom exceptions for specific programming scenarios. For example, you might want to show a specific error when the number is divided by 5.
  4. Don’t throw errors that are actually not errors. For example, if we fetch some data from the database and no records are returned, then this isn’t an error. An empty record list is not an error. So avoid using try-catch here and just add appropriate empty and non-empty checks in your code.

People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level French certification exams to test the ability to communicate in the French language.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri