{"id":149,"date":"2026-05-20T15:37:05","date_gmt":"2026-05-20T15:37:05","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=149"},"modified":"2026-05-21T14:42:11","modified_gmt":"2026-05-21T14:42:11","slug":"try-catch","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=try-catch","title":{"rendered":"try, catch"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Try Catch in PHP is used for exception handling. It helps developers manage errors in a controlled way without stopping the entire application. Using Try Catch improves application stability and provides better error messages for debugging.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Exception handling is commonly used in file handling, database connections, APIs, and user input validation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand exception handling in PHP<\/li>\n\n\n\n<li>Use try and catch blocks correctly<\/li>\n\n\n\n<li>Handle runtime errors safely<\/li>\n\n\n\n<li>Display custom error messages<\/li>\n\n\n\n<li>Use finally blocks in PHP<\/li>\n\n\n\n<li>Create custom exceptions<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is Exception Handling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Exception handling is a method used to handle unexpected errors during program execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of showing fatal errors to users, PHP allows developers to catch exceptions and respond properly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Syntax of Try Catch<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>try {<br>    \/\/ Code that may cause an exception<br>}<br>catch(Exception $e) {<br>    \/\/ Code to handle the exception<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Try Block<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The try block contains code that may generate an exception.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>try {<br>    $num = 10 \/ 0;<br>}<br>catch(Exception $e) {<br>    echo \"Error occurred\";<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Catch Block<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The catch block handles exceptions generated in the try block.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>try {<br>    throw new Exception(\"Invalid operation\");<br>}<br>catch(Exception $e) {<br>    echo $e-&gt;getMessage();<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Invalid operation<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Multiple Catch Blocks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PHP allows handling different exception types separately.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>try {<br>    \/\/ Code<br>}<br>catch(TypeError $e) {<br>    echo \"Type Error\";<br>}<br>catch(Exception $e) {<br>    echo \"General Exception\";<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Finally Block in PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The finally block always executes whether an exception occurs or not.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>try {<br>    echo \"Try Block\";<br>}<br>catch(Exception $e) {<br>    echo \"Catch Block\";<br>}<br>finally {<br>    echo \"Finally Block\";<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Throwing Exceptions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The throw keyword is used to generate exceptions manually.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$age = 15;<br><br>try {<br>    if($age &lt; 18) {<br>        throw new Exception(\"Age must be 18 or above\");<br>    }<br>}<br>catch(Exception $e) {<br>    echo $e-&gt;getMessage();<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Custom Exceptions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Developers can create their own exception classes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class CustomException extends Exception {<br>}<br><br>try {<br>    throw new CustomException(\"Custom Error Message\");<br>}<br>catch(CustomException $e) {<br>    echo $e-&gt;getMessage();<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of Try Catch<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prevents application crashes<\/li>\n\n\n\n<li>Improves user experience<\/li>\n\n\n\n<li>Helps debug applications<\/li>\n\n\n\n<li>Makes code more secure<\/li>\n\n\n\n<li>Simplifies error handling<\/li>\n\n\n\n<li>Provides professional error management<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Common Use Cases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Try Catch is commonly used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Database connections<\/li>\n\n\n\n<li>File handling<\/li>\n\n\n\n<li>API integration<\/li>\n\n\n\n<li>Form validation<\/li>\n\n\n\n<li>Login systems<\/li>\n\n\n\n<li>Payment gateways<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use meaningful error messages<\/li>\n\n\n\n<li>Avoid displaying sensitive system errors to users<\/li>\n\n\n\n<li>Use finally blocks for cleanup operations<\/li>\n\n\n\n<li>Handle specific exceptions separately<\/li>\n\n\n\n<li>Log errors for debugging purposes<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>try {<br>    $conn = new PDO(\"mysql:host=localhost;dbname=test\", \"root\", \"\");<br><br>    echo \"Database Connected\";<br>}<br>catch(PDOException $e) {<br>    echo \"Connection Failed: \" . $e-&gt;getMessage();<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Try Catch in PHP is an essential feature for managing errors and exceptions effectively. It improves application reliability, security, and user experience by handling unexpected problems in a controlled manner.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced PHP > Error Handling > try, catch<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779291429005\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":50,"template":"","class_list":["post-149","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>try, catch - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn PHP Try Catch exception handling with examples, error management, custom exceptions, and finally blocks easily.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=try-catch\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"try, catch - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn PHP Try Catch exception handling with examples, error management, custom exceptions, and finally blocks easily.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=try-catch\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:42:11+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=try-catch\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=try-catch\",\"name\":\"try, catch - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T15:37:05+00:00\",\"dateModified\":\"2026-05-21T14:42:11+00:00\",\"description\":\"Learn PHP Try Catch exception handling with examples, error management, custom exceptions, and finally blocks easily.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=try-catch#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=try-catch\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=try-catch#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced PHP > Error Handling > try, catch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"try, catch - Learn PHP with GiGz.PK","description":"Learn PHP Try Catch exception handling with examples, error management, custom exceptions, and finally blocks easily.","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:\/\/gigz.pk\/php\/?lesson=try-catch","og_locale":"en_US","og_type":"article","og_title":"try, catch - Learn PHP with GiGz.PK","og_description":"Learn PHP Try Catch exception handling with examples, error management, custom exceptions, and finally blocks easily.","og_url":"https:\/\/gigz.pk\/php\/?lesson=try-catch","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:42:11+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=try-catch","url":"https:\/\/gigz.pk\/php\/?lesson=try-catch","name":"try, catch - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T15:37:05+00:00","dateModified":"2026-05-21T14:42:11+00:00","description":"Learn PHP Try Catch exception handling with examples, error management, custom exceptions, and finally blocks easily.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=try-catch#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=try-catch"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=try-catch#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Advanced PHP > Error Handling > try, catch"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}