create account

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

View this thread on: hive.blogpeakd.comecency.com
· @alexendre-maxim ·
$9.09
PHP Tutorial #29 Exception (Try/Catch, Creating a custom exception class, Multiple exceptions and Set a top level exception handler)
<html>
<p><img src="https://cdn.steemitimages.com/DQmR2CdPgUAaypJwbE8QhahqDxrabLTM5fHq8ZjTPcVr2fg/bbva-open4u-herramientas-basicas-desarrolladores-php_1.jpg" width="2122" height="1415"/></p>
<p><a href="https://cdn.steemitimages.com/DQmR2CdPgUAaypJwbE8QhahqDxrabLTM5fHq8ZjTPcVr2fg/bbva-open4u-herramientas-basicas-desarrolladores-php_1.jpg">Image Source</a></p>
<h2>Repository</h2>
<p><a href="https://github.com/php/php-src">https://github.com/php/php-src</a> &nbsp;</p>
<h2>What Will I Learn?</h2>
<ul>
  <li>You will learn the " Try, Throw and Catch ".</li>
  <li>You will learn " How to create a custom exception class " .</li>
  <li>You will learn " Multiple Exceptions " .</li>
  <li>You will learn " How to set a top level exception handler ".</li>
</ul>
<h2>Requirements</h2>
<h5>System Requirements:</h5>
<ul>
  <li>Server support PHP , <a href="https://www.apachefriends.org/fr/download.html">Xampp</a> or <a href="http://www.wampserver.com/">Wamp</a> for example</li>
  <li>An IDE like <a href="https://www.sublimetext.com/download">Sublime text</a>.</li>
  <li>Browser (<a href="https://www.google.com/intl/fr_ALL/chrome/">Chrome</a> for example)&nbsp;&nbsp;</li>
</ul>
<h5>OS Support for PHP</h5>
<ul>
  <li>Windows</li>
  <li>macOS</li>
  <li>Linux</li>
</ul>
<h4>Required Knowledge</h4>
<ul>
  <li>HTML language</li>
  <li>CSS language</li>
</ul>
<h2>Difficulty</h2>
<ul>
  <li>&nbsp;Intermediate&nbsp;</li>
</ul>
<h2>Description</h2>
<p>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 ' .</p>
<p>It generates a fatal error, an unhandled exception automatically generates a fatal error. We will see later what it means " to catch ".</p>
<p>it return Uncaught exception with message, today we will learn how to handle the exception and how to controle these messages.</p>
<h3>1- Try, throw and catch&nbsp;</h3>
<p>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.</p>
<p>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.</p>
<p><img src="https://cdn.steemitimages.com/DQmXr2qGo31UayhwsvRJPwc29dPkqq924NQKRLzYXUgeWs1/1.jpg" width="945" height="436"/></p>
<h3>2- Creating a custom exception class</h3>
<p>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.</p>
<p>For that we will create a class inherited of the class "<code>Exception</code>" we will use the inheritance, the concept of inheritance is very known we will learn it in the next tutorial.</p>
<p>Since this class inherited from the old class '<code>Exception</code>', it takes by default all the methods of the parent class like 'getLine (), getFile () ..etc' that we will use in our own function.</p>
<p><img src="https://cdn.steemitimages.com/DQmQQfkCSLUfjmgrTN5hxeQAd1cFAeEyCB927iNH6E8q7kj/2.jpg" width="945" height="488"/></p>
<h2>3- Multiple exceptions</h2>
<p>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 '.</p>
<p>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.</p>
<p>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 " <code>lessException </code>" and the second one of the class " <code>greaterException</code> " , and we will have two catches, two classes and two different functions.</p>
<p><img src="https://cdn.steemitimages.com/DQmQaKun8bLRYLYPjCZyPTKC8bWb9ouR7Z6URYvYWWB1xoe/3.jpg" width="945" height="560"/></p>
<h3>4- Set a top level exception handler</h3>
<p>To display a general form for all the exceptions we must use the " <code>set_exception_handler()</code> " method, as we have learned how to create a custom exception with an inheritance of the class " <code>Exception</code> ", now we will see how to form a message with just one function the " <code>set_error_handler()</code> ".</p>
<p>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.</p>
<p>Before executing this function we must use " <code>set_exception_handler()</code> " to add this function or this exception to the old ones, and we will use the " <code>throw new Exception</code>" with a message to use it in the function.</p>
<p><img src="https://cdn.steemitimages.com/DQmUob9VtYoK1WEEZmBs32fG4JYguLBuyR8hWV7naJApjLz/4.jpg" width="945" height="436"/></p>
<h2>Video Tutorial</h2>
<p>https://www.youtube.com/watch?v=9MQUCjnszpw&amp;feature=youtu.be</p>
<h2>Curriculum</h2>
<ul>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-11-string-methods-addslaches-chr-chop-and-chunksplite">PHP Tutorial #11 String Methods ( Addslaches, Chr, Chop and Chunk_splite )</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-12-string-methods-bin2hex-countchars-explode-and-hex2bin">PHP Tutorial #12 String Methods ( Bin2Hex, Count_Chars, Explode and Hex2Bin )</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-13-string-methods-implode-join-lcfirst-ltrim-and-fprintf">PHP Tutorial #13 String Methods (Implode, Join, Lcfirst, Ltrim and Fprintf )</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-14-string-methods-md5file-md5-nl2br-and-addcslaches">PHP Tutorial #14 String Methods (Md5_File, Md5, Nl2br and Addcslaches )</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-15-string-methods-numberformat-ord-parsestr-print-and-printf">PHP Tutorial #15 String Methods (Number_Format, Ord, Parse_Str, Print and Printf )</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-16-string-methods-quotemeta-rtrim-sha1file-and-sha1">PHP Tutorial #16 String Methods (QuoteMeta, Rtrim , Sha1_file and Sha1 )</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-17-mathematical-methods-abs-acos-acosh-and-asin">PHP Tutorial #17 Mathematical Methods (Abs, Acos , Acosh and Asin )</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-18-get-and-post-methods-and-usdserver-variables">PHP Tutorial #18 ( GET and POST ) Methods and $_SERVER Variables</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-19-include-and-require-globals-variable-request-and-usdserver-variables">PHP Tutorial #19 ( Include and Require ) , Globals Variable, Request and $_SERVER Variables</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-20-the-cookie-how-to-create-modify-and-delete-a-cookie">PHP Tutorial #20 The Cookie How To Create , Modify And Delete A Cookie</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-21-the-session-how-to-create-modify-and-delete-a-session">PHP Tutorial #21 The Session How To Create , Modify And Delete A Session</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-22-the-outputbuffer-and-how-to-control-it">PHP Tutorial #22 The Output_Buffer And How To Control It</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-23-filters-part-1-filtervalidate">PHP Tutorial #23 Filters Part 1 " FILTER_VALIDATE "</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-24-filters-part-2-filtersanitize">PHP Tutorial #24 Filters Part 2 " FILTER_SANITIZE "</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-25-file-system-part-1-fopen-fread-fwrite-and-filegetcontents">PHP Tutorial #25 File System Part 1 " Fopen, Fread, Fwrite and File_get_contents "</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-26-file-system-part-2-and-filter-part-3">PHP Tutorial #26 File System Part 2 and Filter Part 3</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-27-the-date-timestamp-date-timezone-and-strtotime">PHP Tutorial #27 The Date ( timestamp, date, timezone and strtotime )</a></li>
  <li><a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-28-error-handling-die-create-a-custom-error-trigger-an-error-and-the-use-of-exceptions">PHP Tutorial #28 Error Handling ( die, create a custom error, trigger an error and the use of exceptions )</a></li>
</ul>
<h2>Proof of Work Done</h2>
<p>https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/exceptions.php</p>
</html>
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authoralexendre-maxim
permlinkphp-tutorial-29-exception-try-catch-creating-a-custom-exception-class-multiple-exceptions-and-set-a-top-level-exception-handler
categoryutopian-io
json_metadata{"tags":["utopian-io","video-tutorials","video","programming"],"image":["https://cdn.steemitimages.com/DQmR2CdPgUAaypJwbE8QhahqDxrabLTM5fHq8ZjTPcVr2fg/bbva-open4u-herramientas-basicas-desarrolladores-php_1.jpg","https://cdn.steemitimages.com/DQmXr2qGo31UayhwsvRJPwc29dPkqq924NQKRLzYXUgeWs1/1.jpg","https://cdn.steemitimages.com/DQmQQfkCSLUfjmgrTN5hxeQAd1cFAeEyCB927iNH6E8q7kj/2.jpg","https://cdn.steemitimages.com/DQmQaKun8bLRYLYPjCZyPTKC8bWb9ouR7Z6URYvYWWB1xoe/3.jpg","https://cdn.steemitimages.com/DQmUob9VtYoK1WEEZmBs32fG4JYguLBuyR8hWV7naJApjLz/4.jpg","https://img.youtube.com/vi/9MQUCjnszpw/0.jpg"],"links":["https://cdn.steemitimages.com/DQmR2CdPgUAaypJwbE8QhahqDxrabLTM5fHq8ZjTPcVr2fg/bbva-open4u-herramientas-basicas-desarrolladores-php_1.jpg","https://github.com/php/php-src","https://www.apachefriends.org/fr/download.html","http://www.wampserver.com/","https://www.sublimetext.com/download","https://www.google.com/intl/fr_ALL/chrome/","https://www.youtube.com/watch?v=9MQUCjnszpw&feature=youtu.be","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-11-string-methods-addslaches-chr-chop-and-chunksplite","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-12-string-methods-bin2hex-countchars-explode-and-hex2bin","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-13-string-methods-implode-join-lcfirst-ltrim-and-fprintf","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-14-string-methods-md5file-md5-nl2br-and-addcslaches","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-15-string-methods-numberformat-ord-parsestr-print-and-printf","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-16-string-methods-quotemeta-rtrim-sha1file-and-sha1","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-17-mathematical-methods-abs-acos-acosh-and-asin","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-18-get-and-post-methods-and-usdserver-variables","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-19-include-and-require-globals-variable-request-and-usdserver-variables","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-20-the-cookie-how-to-create-modify-and-delete-a-cookie","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-21-the-session-how-to-create-modify-and-delete-a-session","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-22-the-outputbuffer-and-how-to-control-it","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-23-filters-part-1-filtervalidate","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-24-filters-part-2-filtersanitize","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-25-file-system-part-1-fopen-fread-fwrite-and-filegetcontents","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-26-file-system-part-2-and-filter-part-3","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-27-the-date-timestamp-date-timezone-and-strtotime","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-28-error-handling-die-create-a-custom-error-trigger-an-error-and-the-use-of-exceptions","https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/exceptions.php"],"app":"steemit/0.1","format":"html"}
created2019-04-11 23:54:09
last_update2019-04-11 23:54:09
depth0
children5
last_payout2019-04-18 23:54:09
cashout_time1969-12-31 23:59:59
total_payout_value6.994 HBD
curator_payout_value2.098 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,053
author_reputation18,071,828,077,109
root_title"PHP Tutorial #29 Exception (Try/Catch, Creating a custom exception class, Multiple exceptions and Set a top level exception handler)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,914,831
net_rshares15,247,745,640,130
author_curate_reward""
vote details (62)
@athconnect ·
Youtube is truly a dated platform. Great quality reading dwelling on the world of programming.
properties (22)
authorathconnect
permlinkre-alexendre-maxim-php-tutorial-29-exception-try-catch-creating-a-custom-exception-class-multiple-exceptions-and-set-a-top-level-exception-handler-20190412t005318720z
categoryutopian-io
json_metadata{}
created2019-04-12 00:53:21
last_update2019-04-12 00:53:21
depth1
children0
last_payout2019-04-19 00:53:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length94
author_reputation29,422,719,219
root_title"PHP Tutorial #29 Exception (Try/Catch, Creating a custom exception class, Multiple exceptions and Set a top level exception handler)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,917,631
net_rshares0
@steem-ua ·
$0.04
#### 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](https://discord.gg/KpBNYGz)**
πŸ‘  
properties (23)
authorsteem-ua
permlinkre-php-tutorial-29-exception-try-catch-creating-a-custom-exception-class-multiple-exceptions-and-set-a-top-level-exception-handler-20190414t072821z
categoryutopian-io
json_metadata"{"app": "beem/0.20.19"}"
created2019-04-14 07:28:21
last_update2019-04-14 07:28:21
depth1
children0
last_payout2019-04-21 07:28:21
cashout_time1969-12-31 23:59:59
total_payout_value0.028 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length294
author_reputation23,214,230,978,060
root_title"PHP Tutorial #29 Exception (Try/Catch, Creating a custom exception class, Multiple exceptions and Set a top level exception handler)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id83,063,787
net_rshares61,432,918,801
author_curate_reward""
vote details (1)
@utopian-io ·
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](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

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

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-php-tutorial-29-exception-try-catch-creating-a-custom-exception-class-multiple-exceptions-and-set-a-top-level-exception-handler-20190414t200646z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-04-14 20:06:48
last_update2019-04-14 20:06:48
depth1
children0
last_payout2019-04-21 20:06:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length597
author_reputation152,955,367,999,756
root_title"PHP Tutorial #29 Exception (Try/Catch, Creating a custom exception class, Multiple exceptions and Set a top level exception handler)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id83,097,218
net_rshares0
@yokunjon · (edited)
$5.09
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](https://join.utopian.io/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](https://review.utopian.io/result/9/1-2-2-3-3-3-2-3-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , , , , , , , , ,
properties (23)
authoryokunjon
permlinkre-alexendre-maxim-php-tutorial-29-exception-try-catch-creating-a-custom-exception-class-multiple-exceptions-and-set-a-top-level-exception-handler-20190414t064436490z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/9/1-2-2-3-3-3-2-3-3-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2019-04-14 06:44:36
last_update2019-04-14 06:50:27
depth1
children1
last_payout2019-04-21 06:44:36
cashout_time1969-12-31 23:59:59
total_payout_value3.902 HBD
curator_payout_value1.191 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,685
author_reputation19,266,807,595,513
root_title"PHP Tutorial #29 Exception (Try/Catch, Creating a custom exception class, Multiple exceptions and Set a top level exception handler)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id83,062,042
net_rshares8,258,489,306,898
author_curate_reward""
vote details (18)
@utopian-io ·
Thank you for your review, @yokunjon! Keep up the good work!
πŸ‘  
properties (23)
authorutopian-io
permlinkre-re-alexendre-maxim-php-tutorial-29-exception-try-catch-creating-a-custom-exception-class-multiple-exceptions-and-set-a-top-level-exception-handler-20190414t064436490z-20190416t173822z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-04-16 17:38:24
last_update2019-04-16 17:38:24
depth2
children0
last_payout2019-04-23 17:38:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length60
author_reputation152,955,367,999,756
root_title"PHP Tutorial #29 Exception (Try/Catch, Creating a custom exception class, Multiple exceptions and Set a top level exception handler)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id83,213,094
net_rshares19,510,170,810
author_curate_reward""
vote details (1)