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

in utopian-io •  6 years ago 

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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...

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!