Learn PHP from Scratch Day -13

Learn PHP from Scratch Day -13
Learn PHP from Scratch Day -13

PHP Try Catch Example: Exception & Error Handling Tutorial

What is an Exception?

An error is an unexpected program result that cannot be managed by the program .

Errors are resolved by fixing the app. A good example of an error would be an infinite loop which never stops executing.

An exception is unexpected program result which may be dealt with by the program itself.

Examples of exception include trying to open a document that does not exist.

This exception can be handled by either creating the file or presenting the user with an option of searching for the file.

Why handle exception?

If we not handle the exception then the end user may face the unaccepted result. We try to give better optimized output to the client , but that can't be achieved if they face the exceptions. Let's say division code, we must handle divide by zero error although there is no probability that user will input zero for division, still we should consider it. cause some exceptions may lead to failure of the system. While other exceptions leads to infinite use of resources locking problem.

So any how the exceptions are not beneficial rather they cause the trouble in execution of program.

Another reason to handle exception, is the security , if exception not handled it might throw some sensitive information like login details , tokens or database credentials , which may help increase the security loopholes in the system.

PHP Error handling

When an error occurs, depending on your configuration settings, PHP displays the error message in the web browser with information relating to the error that occurred.

PHP offers a number of ways to handle errors.

We are going to look at three (3) commonly used methods;

  1. Die statements– the die function is the way to avoid the execution of such program that may lead to error in future. die(); stops execution of php script. also it is used for debugging with echo and var_dump().
  2. Custom error handlers — these are programmer defined functions that are called whenever an error occurs. Instead of showing PHP error reporting we show some custom error handle code, that make abstract layer with sensitive information and end user. also it hides the complex coding info that user don't want to see or interested.
  3. PHP error reporting — the error message depending on your PHP error reporting settings. This method is very useful in development environment when you have no idea what caused the error. The information displayed can help you debug your application.

Error handling examples

Lets take example of divided by zero

The code below implements the above example.

which shows the Warning "Divide by zero in line …"

so we have to avoid this error, we have to check whether $denominator is zero or not, so we can handle it as below

We will modify the above code and write an error handler for the application

Let's look at another example that uses a custom error handler.

The custom error handler will be set as the default PHP error handling function and will basically display an error number and message.

The code below illustrates the implementation of the above example.

The output will get

Opps, something went wrong: Error number : [2 ] Error Description: [Divided by zero]

As you can see from the above example, custom error handlers are powerful in the sense that

  • They allow us to customize the error messages.
  • The custom error handler can also include error logging in a file/database, emailing the developer etc.

Let's now look at the third type of error handling. We will be using the PHP built in function error_reporting function. It has the following basic syntax

]

HERE,

  • "error_reporting" is the PHP error reporting function
  • "$reporting_level" is optional, can be used to set the reporting level. If no reporting level has been specified, PHP will use the default error reporting level as specified in the php.ini file.

Difference between Errors and Exception

  • Exceptions are thrown and intended to be caught while errors are generally irrecoverable.
  • Exceptions are handled in an object oriented way.
  • This means when an exception is thrown; an exception object is created that contains the exception details.

The table below shows the exception object methods

Below is the basic syntax for throwing an exception.

HERE,

  • "throw" is the keyword used to throw the exception
  • "new Exception(…)" creates an exception object and passes "This is an exception example " string as the message parameter.

Multiple Exceptions

Multiple exception use multiple try catch blocks to handle the thrown exceptions. Multiple exceptions are useful when;

  • You want to display a customized message depending on the exception thrown
  • You want to perform a unique operation depending on the exception thrown

The flowchart below illustrates the how multiple exceptions work

Summary

  • Errors are unexpected results produced by PHP code
  • Error handling improves the application performance
  • PHP has built in functions that can be used to customize the way PHP reports errors
  • Exceptions are like errors, but they can be caught using the catch block when thrown.
  • Displaying error messages that show error information is considered a bad security practice.