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:
- Server support PHP , Xampp or Wamp for example
- An IDE like Sublime text.
- Browser (Chrome for example)
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
- PHP Tutorial #11 String Methods ( Addslaches, Chr, Chop and Chunk_splite )
- PHP Tutorial #12 String Methods ( Bin2Hex, Count_Chars, Explode and Hex2Bin )
- PHP Tutorial #13 String Methods (Implode, Join, Lcfirst, Ltrim and Fprintf )
- PHP Tutorial #14 String Methods (Md5_File, Md5, Nl2br and Addcslaches )
- PHP Tutorial #15 String Methods (Number_Format, Ord, Parse_Str, Print and Printf )
- PHP Tutorial #16 String Methods (QuoteMeta, Rtrim , Sha1_file and Sha1 )
- PHP Tutorial #17 Mathematical Methods (Abs, Acos , Acosh and Asin )
- PHP Tutorial #18 ( GET and POST ) Methods and $_SERVER Variables
- PHP Tutorial #19 ( Include and Require ) , Globals Variable, Request and $_SERVER Variables
- PHP Tutorial #20 The Cookie How To Create , Modify And Delete A Cookie
- PHP Tutorial #21 The Session How To Create , Modify And Delete A Session
- PHP Tutorial #22 The Output_Buffer And How To Control It
- PHP Tutorial #23 Filters Part 1 " FILTER_VALIDATE "
- PHP Tutorial #24 Filters Part 2 " FILTER_SANITIZE "
- PHP Tutorial #25 File System Part 1 " Fopen, Fread, Fwrite and File_get_contents "
- PHP Tutorial #26 File System Part 2 and Filter Part 3
- PHP Tutorial #27 The Date ( timestamp, date, timezone and strtotime )
- PHP Tutorial #28 Error Handling ( die, create a custom error, trigger an error and the use of exceptions )
Proof of Work Done
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/exceptions.php
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Youtube is truly a dated platform. Great quality reading dwelling on the world of programming.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit