PHP Tutorial #28 Error Handling ( die, create a custom error, trigger an error and the use of exceptions )

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

  • You will learn the die method.
  • You will learn how to create a custom error.
  • You will learn how to trigger an error.
  • You will learn the use of exceptions.

Requirements

System Requirements:
OS Support for PHP
  • Windows
  • macOS
  • Linux

Required Knowledge

  • HTML language
  • CSS language

Difficulty

  •  Intermediate 

Description

In this tutorial we will learn the errors, so an error is defined as an anomaly in the application so we can say that there is an error if the program does not work as it was designed.

During the development an error can come for example from the non respect of the syntax of the language, a bad configuration.. etc.

It can also be an error in the algorithm itself, so our code can generate for example " negative value prices or something like that ", this drives us to be very careful during the design phase.

From error detection and from information to the correction of these errors, Php offers us the possibility to better manage these errors.

Today we will see the " die method, creating a custom error handler, trigger an error and the use of the exceptions ".

1- Die Method

To handle the errors we can use the function " die() ", the word "die" in English that's to say a thing was alive and it blocks it dead, it has no effect now.

If we apply this rule to the errors if we have an error, using the " die() " function we will display a message and we will stop the script execution.

2- Creating a custom error handler

To create a custom error for an error occurs in your site or your application, PHP offers a very efficient idea that is the " Custom Error Handler ".

To create an error you must use a function that accepts 5 parameters, 2 that are very important, and 3 that are optional.

  • The first parameter is the level of error that is a number, we will look at the error levels.
  • The second parameter is the message, that is the message that will be displayed in the page for the user.
  • The third is the filename or file path where we have the error.
  • The fourth is the line where we have the error.
  • Finally the context that is an array, we will see an example to understand them.

After that we will use the function "error_set_handler() " that it accepts the function that we created it to execute this function if there is this error.

The type of an error is represented by a PHP constant.

  • Informative message (constant: E_NOTICE)
  • Alert message (Constant: E_WARNING)
  • Critical Errors (Constant: E_ERROR  )
  • Syntax errors (Constant E_PARSE)
  • PHP internal errors (Constants: E_CORE_ERROR and E_CORE_WARNING)
  • Compilation errors (Constants: E_COMPILE_ERROR and E_COMPILE_WARNING)
  • User Errors (E_USER_ERROR, E_WARNING, E_NOTICE)
  • E_ALL

3- Trigger an error

To trigger an error in your page or application, PHP gives us a " trigger_error() " function, this function accepts 2 parameters :

- The first parameter is the message that will be displayed.

- The second parameter is the level of error, we have 3 levels:

  • E_USER_ERROR.
  • E_USER_WARNING.
  • E_USER_NOTICE.

We can trigger the error but we must first create the error or specify the error as the example we have already done.

4- Use of exceptions

Currently you know fatal errors, alerts, analysis errors or notices. We will discover a different way to handle errors, We will in a way create our own types of errors.

Exceptions are quite different errors that do not work the same way, as you will see this new way of dealing with errors is quite practical.

To throw the exception we will use the " throw() " function with the message that will be displayed in the page for the user.

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/errors.php

Sort:  

Hi @alexendre-maxim

Thank you for another video which adds on to this tutorial series.

The presentation of your video is improving each time as you have more practice and letting your delivery flow from one point to the other.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you @rosatravels, I will try to continue with this method.

Thank you for your review, @rosatravels! Keep up the good work!

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @alexendre-maxim! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 200 upvotes. Your next target is to reach 300 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64534.17
ETH 3150.15
USDT 1.00
SBD 4.01