{"id":148,"date":"2026-05-21T07:45:42","date_gmt":"2026-05-21T07:45:42","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=148"},"modified":"2026-05-24T15:04:46","modified_gmt":"2026-05-24T15:04:46","slug":"try-catch-throw","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=try-catch-throw","title":{"rendered":"try, catch, throw"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Exception handling in C++ is used to manage runtime errors safely. The keywords <code>try<\/code>, <code>catch<\/code>, and <code>throw<\/code> help prevent program crashes by handling unexpected situations properly.<\/p>\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 mechanism that detects and handles runtime errors during program execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of stopping the program suddenly, exceptions allow errors to be handled safely.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Exception Handling is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Exception handling is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prevents program crashes<\/li>\n\n\n\n<li>Handles runtime errors safely<\/li>\n\n\n\n<li>Improves program reliability<\/li>\n\n\n\n<li>Makes debugging easier<\/li>\n\n\n\n<li>Improves user experience<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Keywords Used in Exception Handling<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Keyword<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>try<\/code><\/td><td>Contains code that may generate an error<\/td><\/tr><tr><td><code>throw<\/code><\/td><td>Generates an exception<\/td><\/tr><tr><td><code>catch<\/code><\/td><td>Handles the exception<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Syntax of try, catch, throw<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>try {<br><br>    \/\/ code that may cause error<br><br>    throw exception;<br>}<br><br>catch (type variable) {<br><br>    \/\/ code to handle error<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Basic Example of Exception Handling<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int a = 10, b = 0;<br><br>    try {<br><br>        if (b == 0) {<br><br>            throw \"Division by zero error\";<br>        }<br><br>        cout &lt;&lt; a \/ b;<br>    }<br><br>    catch (const char* msg) {<br><br>        cout &lt;&lt; msg;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Division by zero error<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Exception Handling Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Code inside <code>try<\/code> block executes<\/li>\n\n\n\n<li>Error condition is detected<\/li>\n\n\n\n<li><code>throw<\/code> sends exception<\/li>\n\n\n\n<li>Control moves to <code>catch<\/code> block<\/li>\n\n\n\n<li>Error is handled safely<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">Example with Integer Exception<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    try {<br><br>        throw 100;<br>    }<br><br>    catch (int x) {<br><br>        cout &lt;&lt; \"Exception caught: \" &lt;&lt; x;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Exception caught: 100<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Multiple catch Blocks<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">C++ allows handling different exception types using multiple <code>catch<\/code> blocks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    try {<br><br>        throw 10;<br>    }<br><br>    catch (int x) {<br><br>        cout &lt;&lt; \"Integer exception: \" &lt;&lt; x &lt;&lt; endl;<br>    }<br><br>    catch (...) {<br><br>        cout &lt;&lt; \"Unknown exception\";<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Integer exception: 10<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Example with User Input<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int age;<br><br>    cout &lt;&lt; \"Enter age: \";<br><br>    cin &gt;&gt; age;<br><br>    try {<br><br>        if (age &lt; 18) {<br><br>            throw age;<br>        }<br><br>        cout &lt;&lt; \"Access granted\";<br><br>    }<br><br>    catch (int a) {<br><br>        cout &lt;&lt; \"Access denied. Age: \" &lt;&lt; a;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter age: 15<br>Access denied. Age: 15<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Standard Exception Handling<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">C++ provides standard exceptions through the <code>&lt;exception&gt;<\/code> library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;exception&gt;<br>using namespace std;<br><br>int main() {<br><br>    try {<br><br>        throw runtime_error(\"Runtime error occurred\");<br>    }<br><br>    catch (exception &amp;e) {<br><br>        cout &lt;&lt; e.what();<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Runtime error occurred<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Important Points About Exception Handling<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>try<\/code> block contains risky code<\/li>\n\n\n\n<li><code>throw<\/code> generates exception<\/li>\n\n\n\n<li><code>catch<\/code> handles exception<\/li>\n\n\n\n<li>Multiple exceptions can be handled<\/li>\n\n\n\n<li>Program continues safely after handling<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Common Runtime Errors Handled by Exceptions<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Division by zero<\/li>\n\n\n\n<li>Invalid input<\/li>\n\n\n\n<li>File handling errors<\/li>\n\n\n\n<li>Memory allocation failures<\/li>\n\n\n\n<li>Array out-of-bounds access<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Best Practices for Exception Handling<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use exceptions only for unexpected errors<\/li>\n\n\n\n<li>Write clear error messages<\/li>\n\n\n\n<li>Catch specific exceptions when possible<\/li>\n\n\n\n<li>Avoid unnecessary exception handling<\/li>\n\n\n\n<li>Keep <code>try<\/code> blocks small<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Real-Life Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a traffic system:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Problem occurs on road<\/li>\n\n\n\n<li>Traffic police handle situation<\/li>\n\n\n\n<li>Traffic continues safely<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is similar to exception handling in programming.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Why try, catch, throw are Important<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">These keywords are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Improve software stability<\/li>\n\n\n\n<li>Prevent sudden crashes<\/li>\n\n\n\n<li>Make programs safer<\/li>\n\n\n\n<li>Help manage runtime errors efficiently<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Applications of Exception Handling<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Exception handling is widely used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Banking software<\/li>\n\n\n\n<li>Web applications<\/li>\n\n\n\n<li>Database systems<\/li>\n\n\n\n<li>File management systems<\/li>\n\n\n\n<li>Enterprise applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>try<\/code>, <code>catch<\/code>, and <code>throw<\/code> keywords in C++ provide a powerful way to handle runtime errors safely. Exception handling improves program reliability, prevents crashes, and helps create stable and professional applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced C++ > Exception Handling > try, catch, throw<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":50,"template":"","class_list":["post-148","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>try, catch, throw - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ exception handling using try, catch, and throw with examples. Understand runtime error handling and safer program execution.\" \/>\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\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"try, catch, throw - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ exception handling using try, catch, and throw with examples. Understand runtime error handling and safer program execution.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-24T15:04:46+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\\\/cpp\\\/?lesson=try-catch-throw\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"try, catch, throw - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T07:45:42+00:00\",\"dateModified\":\"2026-05-24T15:04:46+00:00\",\"description\":\"Learn C++ exception handling using try, catch, and throw with examples. Understand runtime error handling and safer program execution.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced C++ > Exception Handling > try, catch, throw\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?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, throw - Learn C++Language with GiGz.PK","description":"Learn C++ exception handling using try, catch, and throw with examples. Understand runtime error handling and safer program execution.","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\/","og_locale":"en_US","og_type":"article","og_title":"try, catch, throw - Learn C++Language with GiGz.PK","og_description":"Learn C++ exception handling using try, catch, and throw with examples. Understand runtime error handling and safer program execution.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-24T15:04:46+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\/cpp\/?lesson=try-catch-throw","url":"https:\/\/gigz.pk\/","name":"try, catch, throw - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T07:45:42+00:00","dateModified":"2026-05-24T15:04:46+00:00","description":"Learn C++ exception handling using try, catch, and throw with examples. Understand runtime error handling and safer program execution.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Advanced C++ > Exception Handling > try, catch, throw"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}