{"id":194,"date":"2018-07-12T12:16:02","date_gmt":"2018-07-12T12:16:02","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=194"},"modified":"2018-12-07T11:08:46","modified_gmt":"2018-12-07T11:08:46","slug":"exception-handling","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/exception-handling\/","title":{"rendered":"Exception Handling"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/tY-dST9s724\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Generally you believe that the program written by you is error free and will execute successfully. However, in few cases the program can terminate while it is executing. For example, if we have written a program which connects to a particular website and download the web pages. In normal condition, the program will execute as we expected but suppose the program runs on a computer which has not internet connection then the program will produce unexpected output. There can be similar situation, where the program open a file to read it, but the file is not exists. Such cases are known as exception. Exception result in abnormal execution and it may lead to abnormally terminate the program.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Exceptions are runtime errors. When writing the code, you should consider any unexpected runtime error. Exception handling allows the program to execute as there is no error in the code or it can terminate the program gracefully.<\/p>\r\n&nbsp;\r\n\r\n<strong>Types of error<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are 2 types of error.\r\n\r\n&nbsp;\r\n\r\n1. Syntax error\r\n\r\n2. Runtime error\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Syntax errors occur when there is the syntax is not correct in the code. For exmplae, instead of echo statement, it is written as ehco.<\/p>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$p = 1000;\r\n\r\n$rate = 10;\r\n\r\n$n = 2;\r\n\r\n$si = ($p*$r*$n)\/100;\r\n\r\nehco \"Simple Interest for Principal $p, rate $rate for duration $n is : $si\";\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThe output is:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"background-color: #ff0000;color: #ffff00\">( ! )<\/span> Parse error: syntax error, unexpected '\"' in H:\\wamp\\www\\PathshalaWAD\\Exception Handling\\1. types of error.php on line <em>6<\/em><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Runtime errors are errors which occur during the execution of program. For example, in the program you want to display the value of array element which does not exists.<\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\">\u00a0 \u00a0 &lt;?php<\/p>\r\n<p style=\"text-align: center\">$cities = array('Ahmedabad','Rajkot','Surat','Vadodara');<\/p>\r\n<p style=\"text-align: center\">$count = count($cities);<\/p>\r\n<p style=\"text-align: center\">for($i=0;$i&lt;=$count;$i++)<\/p>\r\n<p style=\"text-align: center\">{<\/p>\r\n<p style=\"text-align: center\">echo $cities[$i].\"&lt;br&gt;\";<\/p>\r\n<p style=\"text-align: center\">}<\/p>\r\n<p style=\"text-align: center\">?&gt;<\/p>\r\n<p style=\"text-align: center\">The output is:<\/p>\r\n<p style=\"text-align: center\">Ahmedabad<\/p>\r\n<p style=\"text-align: center\">Rajkot<\/p>\r\n<p style=\"text-align: center\">Surat<\/p>\r\n<p style=\"text-align: center\">Vadodara<\/p>\r\n<img class=\"alignnone size-full wp-image-197 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-95.png\" alt=\"\" width=\"642\" height=\"142\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A program may have logical errors. Logical error is a error in which program execute without any error but the output is not as per expectation. For example, in following program as per simple interest formula<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">$si = ($p*$r*$n)\/100 it is divided by 10. Hence though the program execute without any error but instead of correct simple interest 200 it displays 2000.<\/p>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$p = 1000;\r\n\r\n$r = 10;\r\n\r\n$n = 2;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$si = ($p*$r*$n)\/10;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"Simple Interest for Principal $p, interets rate $r for duration $n is : $si\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output is:\r\n\r\n&nbsp;\r\n\r\nSimple Interest for Principal 1000, interets rate 10 for duration 2 is : 2000\r\n\r\n&nbsp;\r\n\r\n<strong>Exception Handling<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Exception handling is used to handle unexpected error conditions. Exception handling is changed the normal flow of the code execution if a specified error (exceptional) occurs.<\/p>\r\n&nbsp;\r\n\r\nException can be thrown from your own exception and then write codes to handle it.\r\n\r\n&nbsp;\r\n\r\nThe syntax for creating a new exception is:\r\n\r\n&nbsp;\r\n\r\nnew Exception ($message [, $code] )\r\n\r\n&nbsp;\r\n\r\n$message is a string. If $message is not string, it is converted to string.\r\n\r\n&nbsp;\r\n\r\n$code is optional. In case $code is not supplied, it is taken as 0(Zero).\r\n\r\n&nbsp;\r\n\r\nThe syntax to throw statement is:\r\n\r\n&nbsp;\r\n\r\nthrow $exception\r\n\r\n&nbsp;\r\n\r\nthrow statement is used to throw an exception object. The throw statement can throw new or existing exception.\r\n\r\n&nbsp;\r\n\r\nWhen an exception is occurred, then\r\n<ul>\r\n \t<li>The current code state is saved<\/li>\r\n \t<li>The code execution will transfer to a predefined exception handler block.<\/li>\r\n \t<li style=\"text-align: justify\">Depending on the situation, the handle take action like terminate the script execution or continue executes the script from different location in the code.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nFollowing code shows example of throw statement.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\nfunction simple($p,$r,$n)\r\n\r\n{\r\n\r\nif($p &lt;= 0 || $r &lt;= 0 ||$n &lt;= 0)\r\n\r\n{\r\n\r\nthrow new Exception(\"Please enter valid values\");\r\n\r\n}\r\n\r\n$si = ($p*$r*$n)\/100;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0echo \"&lt;br&gt; Simple Interest is: $si\";\r\n\r\n}\r\n\r\n$p = 1000;\r\n\r\n$r = 0;\r\n\r\n$n = 2;\r\n\r\n$ans = simple($p,$r,$n);\r\n\r\n?&gt;\r\n\r\n<img class=\"size-full wp-image-198 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-96.png\" alt=\"\" width=\"635\" height=\"218\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nException object has many methods which help to find out more about the exception.\r\n\r\n&nbsp;\r\n\r\nBelow table shows methods of exception object.\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 336px\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 128.063px\"><strong>Method<\/strong><\/td>\r\n<td style=\"height: 28px;width: 424.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 128.063px\">getMessage( )<\/td>\r\n<td style=\"height: 28px;width: 424.063px\">Returns the message set in the exception object<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 128.063px\">getCode( )<\/td>\r\n<td style=\"height: 28px;width: 424.063px\">Returns the code set in the exception object<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 128.063px\">getFile( )<\/td>\r\n<td style=\"height: 28px;width: 424.063px\">Returns the file in which the exception object was thrown<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 128.063px\">getLine( )<\/td>\r\n<td style=\"height: 28px;width: 424.063px\">Returns the line in which the exception object was thrown<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 128.063px\" rowspan=\"3\">getTrace( )\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"height: 28px;width: 424.063px\" rowspan=\"3\">Returns the array having a stack trace for the exception object.\r\n\r\nA stacktrace is a list of functions or methods in reverse ordered\r\n\r\nin which they were called when the exception was thrown.<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 128.063px\">getTraceAsString( )<\/td>\r\n<td style=\"height: 28px;width: 424.063px\">Returns the string having a stack trace for the exception object<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 128.063px\" rowspan=\"3\">_toString( )\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"height: 28px;width: 424.063px\" rowspan=\"3\">Allows to echo an Exception Object. It contains\r\n\r\ninformation of exception object provided as all the above\r\n\r\nmethod.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">When an exception is thrown, it is required to catch it and handle it. In case it is not handles it, the program ends with a runtime error. Try-catch statement is used to catch the exceptions which are thrown.<\/p>\r\n&nbsp;\r\n\r\nSyntax for try-catch statement is shown below:\r\n\r\n&nbsp;\r\n\r\ntry\r\n\r\n{\r\n\r\nstatement(s);\r\n\r\n}\r\n\r\ncatch (ExceptionClass $exception)\r\n\r\n{\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 \u00a0statement(s);\r\n\r\n}\r\n\r\n[ catch (ExceptionClass $exception)\r\n\r\n{\r\n\r\nstatement(s);\r\n\r\n} ]\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In try-catch statement, try block contains the statements which may throw exceptions. In case, any exception does not trigger, the code will be executed as normal.<\/p>\r\n&nbsp;\r\n\r\nThrow statement trigger an exception. Each throw must have at least one catch block.\r\n\r\n&nbsp;\r\n\r\nCatch block contains the statements to execute when an exception is thrown in the try block.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">At the beginning of the catch block, you code the name of the class that defines the exception followed by a variable which allows you to access the exception object.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\nfunction simple($p,$r,$n)\r\n\r\n{\r\n\r\nif($p &lt;= 0 || $r &lt;= 0 ||$n &lt;= 0)\r\n\r\n{\r\n\r\nthrow new exception(\"Please enter valid values\");\r\n\r\n}\r\n\r\n$si = ($p*$r*$n)\/100;\r\n\r\necho \"&lt;br&gt; Simple Interest is: $si\";\r\n\r\n}\r\n\r\n$p = 1000;\r\n\r\n$r = 0;\r\n\r\n$n = 2;\r\n\r\ntry\r\n\r\n{\r\n\r\n$ans = simple($p,$r,$n);\r\n\r\n}\r\n\r\ncatch(Exception $e)\r\n\r\n{\r\n\r\necho \"Error: \".$e-&gt;getMessage();\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThen the output will be:\r\n\r\n&nbsp;\r\n\r\nError: Please enter valid values\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When an exception is thrown, the code following the throw statement will not be executed and PHP try to find the catch block.<\/p>\r\n&nbsp;\r\n\r\nThere must be at-least one catch block for handling exception triggered from try block.\r\n\r\n&nbsp;\r\n\r\n<strong>Creating Custom Exception<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Exception class is the base class for all the exception. It can be inherited to create custom exception object. The custom exception class inherits the properties and methods from Exception class and it can be extended by adding custom functions to it.<\/p>\r\n&nbsp;\r\n\r\nFollowing code shows an example by creating custom exception.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/create custom exception class\r\n\r\nclass customException extends Exception\r\n\r\n{\r\n\r\npublic function errorMessage()\r\n\r\n{\r\n<p style=\"text-align: justify\">$errormsg = \"Error on line \".$this-&gt;getLine().' in &lt;br&gt;File: '.$this-&gt;getFile(). 'Error Message &lt;b&gt;'.$this-\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&gt;getMessage().'&lt;\/b&gt;&lt;br&gt;Please enter valid inputs';<\/p>\r\n<p style=\"text-align: justify\">return $errormsg;<\/p>\r\n}\r\n\r\n}\r\n\r\nfunction simple($p,$r,$n)\r\n\r\n{\r\n\r\nif($p &lt;= 0 || $r &lt;= 0 ||$n &lt;= 0)\r\n\r\n{\r\n\r\nthrow new customException();\r\n\r\n}\r\n\r\n$si = ($p*$r*$n)\/100;\r\n\r\necho \"&lt;br&gt; Simple Interest is: $si\";\r\n\r\n}\r\n\r\n$p = 1000;\r\n\r\n$r = 0;\r\n\r\n$n = 2;\r\n\r\ntry\r\n\r\n{\r\n\r\n$ans = simple($p,$r,$n);\r\n\r\n}\r\n\r\ncatch(Exception $e)\r\n\r\n{\r\n\r\necho \"Custome Exception &lt;br&gt;\".$e-&gt;errorMessage();\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 The output is:\r\n\r\n&nbsp;\r\n\r\nCustome Exception\r\n\r\nError on line 16 in\r\n<p style=\"text-align: justify\">File: H:\\wamp\\www\\PathshalaWAD\\Exception Handling\\6. custome exception.phpError Message Please enter valid inputs<\/p>\r\n&nbsp;\r\n\r\n<strong>Multiple Exceptions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Exception class represents the most general types of exception and its subclasses represent more specific types of errors.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In case you code multiple catch statements, you should code the most specific exception class first and end with the Exception class.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Multiple exception can be used for several if-else blocks or switch statement or to nest multiple exception. Following code shows two exception which shows different error message.<\/p>\r\n&nbsp;\r\n\r\nFollowing code shows an example of multiple exception.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/create custom exception class\r\n\r\nclass customException extends Exception\r\n\r\n{\r\n\r\npublic function errorMessage()\r\n\r\n{\r\n\r\n$errormsg = \"Error on line \".$this-&gt;getLine().' in &lt;br&gt;File: '.$this-&gt;getFile(). 'Error Message &lt;b&gt;'.$this-\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&gt;getMessage().'&lt;\/b&gt;&lt;br&gt;Please enter valid inputs'; return $errormsg;\r\n\r\n}\r\n\r\n}\r\n\r\nfunction simple($p,$r,$n)\r\n\r\n{\r\n\r\nif($p &lt;= 0 AND $r &lt;= 0 AND $n &lt;= 0)\r\n\r\n{\r\n\r\nthrow new customException();\r\n\r\n}\r\n\r\nif($p &lt;= 0 )\r\n\r\n{\r\n\r\nthrow new Exception(\"Please enter valid principal\");\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if ($r &lt;= 0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">throw new Exception(\"Please enter valid rate\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($n &lt;= 0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">throw new Exception(\"Please enter valid duration\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$si = ($p*$r*$n)\/100;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Simple Interest is: $si\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$p = 1000;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$r = 0;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$n = 2;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans = simple($p,$r,$n);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(customException $e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"Custome Exception &lt;br&gt;\".$e-&gt;errorMessage();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch (Exception $e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo $e-&gt;getMessage();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output is:\r\n\r\n&nbsp;\r\n\r\nPlease enter valid rate\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Re-throwing Exceptions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Sometimes, it is required to handle an exception differently than the standard way when an exception is thrown.<\/p>\r\n&nbsp;\r\n\r\nIf necessary, you can code throw statement inside a catch block to re-throw the exception.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For example, A system should hide system errors from users as these errors are not meaningful to him. System errors are important for developer. At that time, exception can be re-thrown to a user with a user friendly message.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/create custom exception class\r\n\r\nclass customException extends Exception\r\n\r\n{\r\n\r\npublic function errorMessage()\r\n\r\n{\r\n\r\n$errormsg\u00a0\u00a0 =\u00a0\u00a0 \"&lt;br&gt;\u00a0\u00a0 Error\u00a0\u00a0 on\u00a0\u00a0 line\u00a0\u00a0 \".$this-&gt;getLine().'\u00a0\u00a0\u00a0 in\u00a0\u00a0 &lt;br&gt;File:\u00a0\u00a0\u00a0\u00a0 '.$this-\r\n\r\n&gt;getFile().\r\n\r\n\"Error Message &lt;b&gt;\".$this-&gt;getMessage().\"&lt;\/b&gt;&lt;br&gt;Please enter valid inputs\"; return $errormsg;\r\n\r\n}\r\n\r\n}\r\n\r\n\/\/ inputs\r\n\r\n$p = 1000;\r\n\r\n$r = 0;\r\n\r\n$n = 2;\r\n\r\n\/\/ function\r\n\r\nfunction simple($p,$r,$n)\r\n\r\n{\r\n\r\n$si = ($p*$r*$n)\/100;\r\n\r\nreturn $si;\r\n\r\n}\r\n\r\ntry\r\n\r\n{\r\n\r\ntry\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($p &lt;= 0 )<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">throw new Exception(\"Please enter valid principal\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if ($r &lt;= 0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">throw new Exception(\"Please enter valid rate\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($n &lt;= 0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">throw new Exception(\"Please enter valid duration\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($p &lt;= 0 || $r &lt;= 0 || $n &lt;= 0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">throw new customException();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception $e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Exception :\".$e-&gt;getMessage();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">throw new customException();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch (customException $e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo $e-&gt;errorMessage();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans = simple($p,$r,$n);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Simple Interest : $ans\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">The output is:<\/span>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Exception :Please enter valid rate<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Error on line 47 in<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">File: H:\\wamp\\www\\PathshalaWAD\\Exception Handling\\8. rethrow exception.phpError Message<\/p>\r\nPlease enter valid inputs\r\n\r\nSimple Interest : 0\r\n\r\n&nbsp;\r\n\r\n<strong>Points to ponder for exception<\/strong>\r\n<ul>\r\n \t<li>Write the executable code in try block to help to catch potential exceptions<\/li>\r\n \t<li>Each try or throw statement must have at least one corresponding catch block.<\/li>\r\n \t<li>Multiple catch blocks can be used to catch various types of exceptions<\/li>\r\n \t<li>Exception can be thrown or re-thrown in a catch block within a try block<\/li>\r\n \t<li>The thumb rule is that if you are throwing something, you have to catch it.<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Exception Handling<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/tY-dST9s724\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<strong>References:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP, Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. www.php.net<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/tY-dST9s724\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Generally you believe that the program written by you is error free and will execute successfully. However, in few cases the program can terminate while it is executing. For example, if we have written a program which connects to a particular website and download the web pages. In normal condition, the program will execute as we expected but suppose the program runs on a computer which has not internet connection then the program will produce unexpected output. There can be similar situation, where the program open a file to read it, but the file is not exists. Such cases are known as exception. Exception result in abnormal execution and it may lead to abnormally terminate the program.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Exceptions are runtime errors. When writing the code, you should consider any unexpected runtime error. Exception handling allows the program to execute as there is no error in the code or it can terminate the program gracefully.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Types of error<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are 2 types of error.<\/p>\n<p>&nbsp;<\/p>\n<p>1. Syntax error<\/p>\n<p>2. Runtime error<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Syntax errors occur when there is the syntax is not correct in the code. For exmplae, instead of echo statement, it is written as ehco.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$p = 1000;<\/p>\n<p>$rate = 10;<\/p>\n<p>$n = 2;<\/p>\n<p>$si = ($p*$r*$n)\/100;<\/p>\n<p>ehco &#8220;Simple Interest for Principal $p, rate $rate for duration $n is : $si&#8221;;<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The output is:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"background-color: #ff0000;color: #ffff00\">( ! )<\/span> Parse error: syntax error, unexpected &#8216;&#8221;&#8216; in H:\\wamp\\www\\PathshalaWAD\\Exception Handling\\1. types of error.php on line <em>6<\/em><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Runtime errors are errors which occur during the execution of program. For example, in the program you want to display the value of array element which does not exists.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\">\u00a0 \u00a0 &lt;?php<\/p>\n<p style=\"text-align: center\">$cities = array(&#8216;Ahmedabad&#8217;,&#8217;Rajkot&#8217;,&#8217;Surat&#8217;,&#8217;Vadodara&#8217;);<\/p>\n<p style=\"text-align: center\">$count = count($cities);<\/p>\n<p style=\"text-align: center\">for($i=0;$i&lt;=$count;$i++)<\/p>\n<p style=\"text-align: center\">{<\/p>\n<p style=\"text-align: center\">echo $cities[$i].&#8221;&lt;br&gt;&#8221;;<\/p>\n<p style=\"text-align: center\">}<\/p>\n<p style=\"text-align: center\">?&gt;<\/p>\n<p style=\"text-align: center\">The output is:<\/p>\n<p style=\"text-align: center\">Ahmedabad<\/p>\n<p style=\"text-align: center\">Rajkot<\/p>\n<p style=\"text-align: center\">Surat<\/p>\n<p style=\"text-align: center\">Vadodara<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-197 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-95.png\" alt=\"\" width=\"642\" height=\"142\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-95.png 642w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-95-300x66.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-95-65x14.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-95-225x50.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-95-350x77.png 350w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A program may have logical errors. Logical error is a error in which program execute without any error but the output is not as per expectation. For example, in following program as per simple interest formula<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">$si = ($p*$r*$n)\/100 it is divided by 10. Hence though the program execute without any error but instead of correct simple interest 200 it displays 2000.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$p = 1000;<\/p>\n<p>$r = 10;<\/p>\n<p>$n = 2;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$si = ($p*$r*$n)\/10;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;Simple Interest for Principal $p, interets rate $r for duration $n is : $si&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The output is:<\/p>\n<p>&nbsp;<\/p>\n<p>Simple Interest for Principal 1000, interets rate 10 for duration 2 is : 2000<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exception Handling<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Exception handling is used to handle unexpected error conditions. Exception handling is changed the normal flow of the code execution if a specified error (exceptional) occurs.<\/p>\n<p>&nbsp;<\/p>\n<p>Exception can be thrown from your own exception and then write codes to handle it.<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax for creating a new exception is:<\/p>\n<p>&nbsp;<\/p>\n<p>new Exception ($message [, $code] )<\/p>\n<p>&nbsp;<\/p>\n<p>$message is a string. If $message is not string, it is converted to string.<\/p>\n<p>&nbsp;<\/p>\n<p>$code is optional. In case $code is not supplied, it is taken as 0(Zero).<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax to throw statement is:<\/p>\n<p>&nbsp;<\/p>\n<p>throw $exception<\/p>\n<p>&nbsp;<\/p>\n<p>throw statement is used to throw an exception object. The throw statement can throw new or existing exception.<\/p>\n<p>&nbsp;<\/p>\n<p>When an exception is occurred, then<\/p>\n<ul>\n<li>The current code state is saved<\/li>\n<li>The code execution will transfer to a predefined exception handler block.<\/li>\n<li style=\"text-align: justify\">Depending on the situation, the handle take action like terminate the script execution or continue executes the script from different location in the code.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Following code shows example of throw statement.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>function simple($p,$r,$n)<\/p>\n<p>{<\/p>\n<p>if($p &lt;= 0 || $r &lt;= 0 ||$n &lt;= 0)<\/p>\n<p>{<\/p>\n<p>throw new Exception(&#8220;Please enter valid values&#8221;);<\/p>\n<p>}<\/p>\n<p>$si = ($p*$r*$n)\/100;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0echo &#8220;&lt;br&gt; Simple Interest is: $si&#8221;;<\/p>\n<p>}<\/p>\n<p>$p = 1000;<\/p>\n<p>$r = 0;<\/p>\n<p>$n = 2;<\/p>\n<p>$ans = simple($p,$r,$n);<\/p>\n<p>?&gt;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-198 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-96.png\" alt=\"\" width=\"635\" height=\"218\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-96.png 635w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-96-300x103.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-96-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-96-225x77.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-96-350x120.png 350w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Exception object has many methods which help to find out more about the exception.<\/p>\n<p>&nbsp;<\/p>\n<p>Below table shows methods of exception object.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 336px\">\n<tbody>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 128.063px\"><strong>Method<\/strong><\/td>\n<td style=\"height: 28px;width: 424.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 128.063px\">getMessage( )<\/td>\n<td style=\"height: 28px;width: 424.063px\">Returns the message set in the exception object<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 128.063px\">getCode( )<\/td>\n<td style=\"height: 28px;width: 424.063px\">Returns the code set in the exception object<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 128.063px\">getFile( )<\/td>\n<td style=\"height: 28px;width: 424.063px\">Returns the file in which the exception object was thrown<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 128.063px\">getLine( )<\/td>\n<td style=\"height: 28px;width: 424.063px\">Returns the line in which the exception object was thrown<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 128.063px\" rowspan=\"3\">getTrace( )<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"height: 28px;width: 424.063px\" rowspan=\"3\">Returns the array having a stack trace for the exception object.<\/p>\n<p>A stacktrace is a list of functions or methods in reverse ordered<\/p>\n<p>in which they were called when the exception was thrown.<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 128.063px\">getTraceAsString( )<\/td>\n<td style=\"height: 28px;width: 424.063px\">Returns the string having a stack trace for the exception object<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 128.063px\" rowspan=\"3\">_toString( )<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"height: 28px;width: 424.063px\" rowspan=\"3\">Allows to echo an Exception Object. It contains<\/p>\n<p>information of exception object provided as all the above<\/p>\n<p>method.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When an exception is thrown, it is required to catch it and handle it. In case it is not handles it, the program ends with a runtime error. Try-catch statement is used to catch the exceptions which are thrown.<\/p>\n<p>&nbsp;<\/p>\n<p>Syntax for try-catch statement is shown below:<\/p>\n<p>&nbsp;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>statement(s);<\/p>\n<p>}<\/p>\n<p>catch (ExceptionClass $exception)<\/p>\n<p>{<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 \u00a0statement(s);<\/p>\n<p>}<\/p>\n<p>[ catch (ExceptionClass $exception)<\/p>\n<p>{<\/p>\n<p>statement(s);<\/p>\n<p>} ]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In try-catch statement, try block contains the statements which may throw exceptions. In case, any exception does not trigger, the code will be executed as normal.<\/p>\n<p>&nbsp;<\/p>\n<p>Throw statement trigger an exception. Each throw must have at least one catch block.<\/p>\n<p>&nbsp;<\/p>\n<p>Catch block contains the statements to execute when an exception is thrown in the try block.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">At the beginning of the catch block, you code the name of the class that defines the exception followed by a variable which allows you to access the exception object.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>function simple($p,$r,$n)<\/p>\n<p>{<\/p>\n<p>if($p &lt;= 0 || $r &lt;= 0 ||$n &lt;= 0)<\/p>\n<p>{<\/p>\n<p>throw new exception(&#8220;Please enter valid values&#8221;);<\/p>\n<p>}<\/p>\n<p>$si = ($p*$r*$n)\/100;<\/p>\n<p>echo &#8220;&lt;br&gt; Simple Interest is: $si&#8221;;<\/p>\n<p>}<\/p>\n<p>$p = 1000;<\/p>\n<p>$r = 0;<\/p>\n<p>$n = 2;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>$ans = simple($p,$r,$n);<\/p>\n<p>}<\/p>\n<p>catch(Exception $e)<\/p>\n<p>{<\/p>\n<p>echo &#8220;Error: &#8220;.$e-&gt;getMessage();<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Then the output will be:<\/p>\n<p>&nbsp;<\/p>\n<p>Error: Please enter valid values<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When an exception is thrown, the code following the throw statement will not be executed and PHP try to find the catch block.<\/p>\n<p>&nbsp;<\/p>\n<p>There must be at-least one catch block for handling exception triggered from try block.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Creating Custom Exception<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Exception class is the base class for all the exception. It can be inherited to create custom exception object. The custom exception class inherits the properties and methods from Exception class and it can be extended by adding custom functions to it.<\/p>\n<p>&nbsp;<\/p>\n<p>Following code shows an example by creating custom exception.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/create custom exception class<\/p>\n<p>class customException extends Exception<\/p>\n<p>{<\/p>\n<p>public function errorMessage()<\/p>\n<p>{<\/p>\n<p style=\"text-align: justify\">$errormsg = &#8220;Error on line &#8220;.$this-&gt;getLine().&#8217; in &lt;br&gt;File: &#8216;.$this-&gt;getFile(). &#8216;Error Message &lt;b&gt;&#8217;.$this-\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&gt;getMessage().'&lt;\/b&gt;&lt;br&gt;Please enter valid inputs&#8217;;<\/p>\n<p style=\"text-align: justify\">return $errormsg;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>function simple($p,$r,$n)<\/p>\n<p>{<\/p>\n<p>if($p &lt;= 0 || $r &lt;= 0 ||$n &lt;= 0)<\/p>\n<p>{<\/p>\n<p>throw new customException();<\/p>\n<p>}<\/p>\n<p>$si = ($p*$r*$n)\/100;<\/p>\n<p>echo &#8220;&lt;br&gt; Simple Interest is: $si&#8221;;<\/p>\n<p>}<\/p>\n<p>$p = 1000;<\/p>\n<p>$r = 0;<\/p>\n<p>$n = 2;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>$ans = simple($p,$r,$n);<\/p>\n<p>}<\/p>\n<p>catch(Exception $e)<\/p>\n<p>{<\/p>\n<p>echo &#8220;Custome Exception &lt;br&gt;&#8221;.$e-&gt;errorMessage();<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 The output is:<\/p>\n<p>&nbsp;<\/p>\n<p>Custome Exception<\/p>\n<p>Error on line 16 in<\/p>\n<p style=\"text-align: justify\">File: H:\\wamp\\www\\PathshalaWAD\\Exception Handling\\6. custome exception.phpError Message Please enter valid inputs<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Multiple Exceptions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Exception class represents the most general types of exception and its subclasses represent more specific types of errors.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In case you code multiple catch statements, you should code the most specific exception class first and end with the Exception class.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Multiple exception can be used for several if-else blocks or switch statement or to nest multiple exception. Following code shows two exception which shows different error message.<\/p>\n<p>&nbsp;<\/p>\n<p>Following code shows an example of multiple exception.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/create custom exception class<\/p>\n<p>class customException extends Exception<\/p>\n<p>{<\/p>\n<p>public function errorMessage()<\/p>\n<p>{<\/p>\n<p>$errormsg = &#8220;Error on line &#8220;.$this-&gt;getLine().&#8217; in &lt;br&gt;File: &#8216;.$this-&gt;getFile(). &#8216;Error Message &lt;b&gt;&#8217;.$this-\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&gt;getMessage().'&lt;\/b&gt;&lt;br&gt;Please enter valid inputs&#8217;; return $errormsg;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>function simple($p,$r,$n)<\/p>\n<p>{<\/p>\n<p>if($p &lt;= 0 AND $r &lt;= 0 AND $n &lt;= 0)<\/p>\n<p>{<\/p>\n<p>throw new customException();<\/p>\n<p>}<\/p>\n<p>if($p &lt;= 0 )<\/p>\n<p>{<\/p>\n<p>throw new Exception(&#8220;Please enter valid principal&#8221;);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if ($r &lt;= 0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">throw new Exception(&#8220;Please enter valid rate&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($n &lt;= 0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">throw new Exception(&#8220;Please enter valid duration&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$si = ($p*$r*$n)\/100;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Simple Interest is: $si&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$p = 1000;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$r = 0;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$n = 2;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans = simple($p,$r,$n);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(customException $e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;Custome Exception &lt;br&gt;&#8221;.$e-&gt;errorMessage();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch (Exception $e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo $e-&gt;getMessage();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The output is:<\/p>\n<p>&nbsp;<\/p>\n<p>Please enter valid rate<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Re-throwing Exceptions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Sometimes, it is required to handle an exception differently than the standard way when an exception is thrown.<\/p>\n<p>&nbsp;<\/p>\n<p>If necessary, you can code throw statement inside a catch block to re-throw the exception.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For example, A system should hide system errors from users as these errors are not meaningful to him. System errors are important for developer. At that time, exception can be re-thrown to a user with a user friendly message.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/create custom exception class<\/p>\n<p>class customException extends Exception<\/p>\n<p>{<\/p>\n<p>public function errorMessage()<\/p>\n<p>{<\/p>\n<p>$errormsg\u00a0\u00a0 =\u00a0\u00a0 &#8220;&lt;br&gt;\u00a0\u00a0 Error\u00a0\u00a0 on\u00a0\u00a0 line\u00a0\u00a0 &#8220;.$this-&gt;getLine().&#8217;\u00a0\u00a0\u00a0 in\u00a0\u00a0 &lt;br&gt;File:\u00a0\u00a0\u00a0\u00a0 &#8216;.$this-<\/p>\n<p>&gt;getFile().<\/p>\n<p>&#8220;Error Message &lt;b&gt;&#8221;.$this-&gt;getMessage().&#8221;&lt;\/b&gt;&lt;br&gt;Please enter valid inputs&#8221;; return $errormsg;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ inputs<\/p>\n<p>$p = 1000;<\/p>\n<p>$r = 0;<\/p>\n<p>$n = 2;<\/p>\n<p>\/\/ function<\/p>\n<p>function simple($p,$r,$n)<\/p>\n<p>{<\/p>\n<p>$si = ($p*$r*$n)\/100;<\/p>\n<p>return $si;<\/p>\n<p>}<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>try<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($p &lt;= 0 )<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">throw new Exception(&#8220;Please enter valid principal&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if ($r &lt;= 0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">throw new Exception(&#8220;Please enter valid rate&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($n &lt;= 0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">throw new Exception(&#8220;Please enter valid duration&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($p &lt;= 0 || $r &lt;= 0 || $n &lt;= 0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">throw new customException();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception $e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Exception :&#8221;.$e-&gt;getMessage();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">throw new customException();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch (customException $e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo $e-&gt;errorMessage();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans = simple($p,$r,$n);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Simple Interest : $ans&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">The output is:<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Exception :Please enter valid rate<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Error on line 47 in<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">File: H:\\wamp\\www\\PathshalaWAD\\Exception Handling\\8. rethrow exception.phpError Message<\/p>\n<p>Please enter valid inputs<\/p>\n<p>Simple Interest : 0<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Points to ponder for exception<\/strong><\/p>\n<ul>\n<li>Write the executable code in try block to help to catch potential exceptions<\/li>\n<li>Each try or throw statement must have at least one corresponding catch block.<\/li>\n<li>Multiple catch blocks can be used to catch various types of exceptions<\/li>\n<li>Exception can be thrown or re-thrown in a catch block within a try block<\/li>\n<li>The thumb rule is that if you are throwing something, you have to catch it.<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Exception Handling<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/tY-dST9s724\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>References:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP, Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\n<p style=\"text-align: justify\">11. www.php.net<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n","protected":false},"author":3,"menu_order":19,"template":"","meta":{"_acf_changed":false,"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-194","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/194\/revisions"}],"predecessor-version":[{"id":504,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/194\/revisions\/504"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/194\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=194"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=194"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=194"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}