{"id":678,"date":"2012-05-15T14:55:52","date_gmt":"2012-05-15T14:55:52","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=678"},"modified":"2012-05-15T14:55:52","modified_gmt":"2012-05-15T14:55:52","slug":"further-php-try-catch-php-5","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/","title":{"rendered":"Further PHP try \/ catch PHP 5"},"content":{"rendered":"<p>A try \/ catch block is meant to catch exceptions.\u00a0 An exception would be something like divide by zero which causes a program exception and this can be caught.<\/p>\n<p>An error on the other hand is not usually recoverable.\u00a0 An example of an error would be forgetting to place a ; at the end of a line or not enclosing a string with &#8221; marks.<\/p>\n<p>In the case of divide by zero, if you use a try \/ catch block, program execution will continue because you have caught the exception.<\/p>\n<p>Each try must have at least one corresponding catch block.\u00a0 You can have multiple catch blocks to catch different classes of exceptions.<\/p>\n<p>When an exception is thrown, the code following the statement will not be executed and PHP will then attempt to find the first matching catch block.<\/p>\n<p>The general form of a try \/ catch block is :<\/p>\n<p>try<br \/>\n{<br \/>\n$a = 1;<br \/>\n$b = 0;<br \/>\n$c = $a \/ $b;<br \/>\n}<br \/>\ncatch (Exception $e)<br \/>\n{<br \/>\necho($e-&gt;getMessage());<br \/>\n}<\/p>\n<p>Other functions of the exception class are :<\/p>\n<p>getMessage();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ message of exception<br \/>\ngetCode();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ code of exception<br \/>\ngetFile();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ source filename<br \/>\ngetLine();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ source line<br \/>\ngetTrace();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ an array of the backtrace()<br \/>\ngetPrevious();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ previous exception<br \/>\ngetTraceAsString();\u00a0 \/\/ formatted string of trace<\/p>\n<p>You may extend the exception class to create your own custom exceptions and the use them as multiple catch blocks to catch different classes of exception as shown in the following code :<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/Extending the exception class<\/p>\n<p>class WidgetNotFoundException extends Exception {}<\/p>\n<p>function use_widget($widget_name) {<br \/>\n$widget = find_widget($widget_name);<\/p>\n<p>if (!$widget) {<br \/>\nthrow new WidgetNotFoundException(t(&#8216;Widget %widget not found.&#8217;, array(&#8216;%widget&#8217; =&gt; $widget_name)));<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/The try \/ catch block<\/p>\n<p>try {<br \/>\n$widget = &#8216;thingie&#8217;;<br \/>\n$result = use_widget($widget);<\/p>\n<p>\/\/ Continue processing the $result.<br \/>\n\/\/ If an exception is thrown by use_widget(), this code never gets called.<br \/>\n}<br \/>\ncatch (WidgetNotFoundException $e) {<br \/>\n\/\/ Error handling specific to the absence of a widget.<br \/>\n}<br \/>\ncatch (Exception $e) {<br \/>\n\/\/ Generic exception handling if something else gets thrown.<br \/>\nwatchdog(&#8216;widget&#8217;, $e-&gt;getMessage(), WATCHDOG_ERROR);<br \/>\n}<\/p>\n<p>?&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A try \/ catch block is meant to catch exceptions.\u00a0 An exception would be something like divide by zero which causes a program exception and this can be caught. An error on the other hand is not usually recoverable.\u00a0 An<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/\">Read more <span class=\"screen-reader-text\">Further PHP try \/ catch PHP 5<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-678","post","type-post","status-publish","format-standard","hentry","category-general-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Further PHP try \/ catch PHP 5 - PHP Web Applications<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Further PHP try \/ catch PHP 5 - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"A try \/ catch block is meant to catch exceptions.\u00a0 An exception would be something like divide by zero which causes a program exception and this can be caught. An error on the other hand is not usually recoverable.\u00a0 An&hellip;Read more Further PHP try \/ catch PHP 5 &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-05-15T14:55:52+00:00\" \/>\n<meta name=\"author\" content=\"paulv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"paulv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Further PHP try \\\/ catch PHP 5\",\"datePublished\":\"2012-05-15T14:55:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/\"},\"wordCount\":322,\"commentCount\":0,\"articleSection\":[\"General PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/\",\"name\":\"Further PHP try \\\/ catch PHP 5 - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-05-15T14:55:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/further-php-try-catch-php-5\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Further PHP try \\\/ catch PHP 5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\",\"name\":\"PHP Web Applications\",\"description\":\"Information and support for products of WithinWeb.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\",\"name\":\"paulv\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/author\\\/paulv\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Further PHP try \/ catch PHP 5 - PHP Web Applications","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/","og_locale":"en_US","og_type":"article","og_title":"Further PHP try \/ catch PHP 5 - PHP Web Applications","og_description":"A try \/ catch block is meant to catch exceptions.\u00a0 An exception would be something like divide by zero which causes a program exception and this can be caught. An error on the other hand is not usually recoverable.\u00a0 An&hellip;Read more Further PHP try \/ catch PHP 5 &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/","og_site_name":"PHP Web Applications","article_published_time":"2012-05-15T14:55:52+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Further PHP try \/ catch PHP 5","datePublished":"2012-05-15T14:55:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/"},"wordCount":322,"commentCount":0,"articleSection":["General PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/","url":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/","name":"Further PHP try \/ catch PHP 5 - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-05-15T14:55:52+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/further-php-try-catch-php-5\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Further PHP try \/ catch PHP 5"}]},{"@type":"WebSite","@id":"https:\/\/www.withinweb.com\/info\/#website","url":"https:\/\/www.withinweb.com\/info\/","name":"PHP Web Applications","description":"Information and support for products of WithinWeb.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.withinweb.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c","name":"paulv","url":"https:\/\/www.withinweb.com\/info\/author\/paulv\/"}]}},"_links":{"self":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/comments?post=678"}],"version-history":[{"count":3,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"predecessor-version":[{"id":684,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/678\/revisions\/684"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}