PHP Tutorial #29 Exception (Try/Catch, Creating a custom exception class, Multiple exceptions and Set a top level exception handler)

Image Source

Repository

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

What Will I Learn?

  • You will learn the " Try, Throw and Catch ".
  • You will learn " How to create a custom exception class " .
  • You will learn " Multiple Exceptions " .
  • You will learn " How to set a top level exception handler ".

Requirements

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

Required Knowledge

  • HTML language
  • CSS language

Difficulty

  •  Intermediate 

Description

In this tutorial we are going to complete the exceptions part, as we have learned from the errors and the base of the use of the exceptions, today we will see the ' Try/Catch, Creating a Custom Exception Class, Multiple Exceptions and Set a Top Level Exception Handler ' .

It generates a fatal error, an unhandled exception automatically generates a fatal error. We will see later what it means " to catch ".

it return Uncaught exception with message, today we will learn how to handle the exception and how to controle these messages.

1- Try, throw and catch 

In order to catch an exception it must first be launched, the problem is that we can't tell PHP that all the exceptions launched must be caugh.

It is up to us to tell it that we will try to carry out this or that instruction, and if an exception is launched, then we will catch it.

2- Creating a custom exception class

As we have seen the try catch block, in this part we will see how to create a custom exception as we have studied how to create a custom error for our site.

For that we will create a class inherited of the class "Exception" we will use the inheritance, the concept of inheritance is very known we will learn it in the next tutorial.

Since this class inherited from the old class 'Exception', it takes by default all the methods of the parent class like 'getLine (), getFile () ..etc' that we will use in our own function.

3- Multiple exceptions

If we have more than one condition or we can say more than one exception, can we manage them? simply "yes", PHP offers the mechanism of ' Multiple Exceptions '.

If we use the example of age if the age is less then 18 we can trigger an exception, but also we have another condition if the age is greater than 150 it's incredible so we have to trigger another exception.

So in the " try " we are going to make two conditions the first will trigger an exception or will create an object of the class for example " lessException " and the second one of the class " greaterException " , and we will have two catches, two classes and two different functions.

4- Set a top level exception handler

To display a general form for all the exceptions we must use the " set_exception_handler() " method, as we have learned how to create a custom exception with an inheritance of the class " Exception ", now we will see how to form a message with just one function the " set_error_handler() ".

We must first create a function with a parameter that is the exception, and in the exception we will use some functions to display the message, the line ..etc.

Before executing this function we must use " set_exception_handler() " to add this function or this exception to the old ones, and we will use the " throw new Exception" with a message to use it in the function.

Video Tutorial

Curriculum

Proof of Work Done

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

Sort:  

I thank you for your contribution. Here are my thoughts. Note that, my thoughts are my personal ideas on your post and they are not directly related to the review and scoring unlike the answers I gave in the questionnaire;

  • Structure

    • The overall structure of your video is good as it was before. I appreciate your effort on that.
  • Language

    • There were some sentences of yours which were structurally broken. It is okay but I just wanted to state it.

    • I managed to understand your tutorial in 2x speed, but still; sometimes it becomes hard to comprehend what you say. It will improve in time and it did. I see good effort on that.

  • Content

    • I suggest considering other free paint programs, which smooths out the brushes. When you draw with this one, it looks ugly from my perspective. It's not a big deal but just consider it.

    • Even you try to simplify things as you teach the concepts, what you've done in the video is an anti-pattern. You are using exceptions for flow-control, which is a bad habit. You can do everything you did with exceptions purely using ifs, switches and returns. Exceptions are required when unpredictable, not expected things happen; so try to use them that way.


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 for your review, @yokunjon! 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

Youtube is truly a dated platform. Great quality reading dwelling on the world of programming.

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!

New to Steemit?

Coin Marketplace

STEEM 0.15
TRX 0.27
JST 0.037
BTC 103676.66
ETH 2620.88
USDT 1.00
SBD 0.90