{"id":1997,"date":"2025-01-26T21:47:56","date_gmt":"2025-01-26T21:47:56","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=1997"},"modified":"2025-01-27T18:55:17","modified_gmt":"2025-01-27T18:55:17","slug":"standard-exceptions","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/exceptions\/standard-exceptions\/","title":{"rendered":"Standard Exceptions"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Standard Exceptions<\/h2>\n\n\n\n<p>The Standard C++ library provides a set of exceptions that are also available for your own use. They also provide a model for how you might construct your own exception hierarchy, perhaps inheriting from a suitable standard library exception class.<\/p>\n\n\n\n<p><strong>exception<\/strong> is the base class for all the Standard C++ library exceptions. It is defined in the header\u00a0<strong>&lt;exception><\/strong>.<\/p>\n\n\n\n<p>The two main classes derived from exception are&nbsp;<strong>logic_error<\/strong>&nbsp;and&nbsp;<strong>runtime_error<\/strong>, which are found in&nbsp;&nbsp;<strong>&lt;stdexcept&gt;<\/strong>&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The class&nbsp;<strong>logic_error<\/strong>&nbsp;represents errors in programming logic, such as passing an invalid argument.<\/li>\n\n\n\n<li>Runtime errors are those that occur as the result of unforeseen forces such as hardware failure or memory exhaustion.<\/li>\n<\/ul>\n\n\n\n<p>Both&nbsp;<strong>runtime_error<\/strong>&nbsp;and&nbsp;<strong>logic_error<\/strong>&nbsp;provide a constructor that takes a&nbsp;<strong>std::string<\/strong>&nbsp;argument so that you can store a message in the exception object and extract it later with&nbsp;<strong>exception::what(&nbsp;)<\/strong>&nbsp;.<\/p>\n\n\n\n<p>A simplified hierarchy looks like this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>exception<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>logic_error<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>invalid_argument<\/strong><\/li>\n\n\n\n<li><strong>domain_error<\/strong><\/li>\n\n\n\n<li><strong>length_error<\/strong><\/li>\n\n\n\n<li><strong>out_of_range<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>runtime_error<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>range_error<\/strong><\/li>\n\n\n\n<li><strong>overflow_error<\/strong><\/li>\n\n\n\n<li><strong>underflow_error<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>bad_alloc<\/strong> &#8212; thrown to indicate failure to allocate storage<\/li>\n\n\n\n<li><strong>bad_cast<\/strong><\/li>\n\n\n\n<li><strong>bad_exception<\/strong><\/li>\n\n\n\n<li><strong>bad_typeid<\/strong><\/li>\n\n\n\n<li><strong>ios_base::failure<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>&#8230; and so on.<\/p>\n\n\n\n<p>The standard library exception classes do not add member functions beyond those provided by <strong>exception<\/strong>, but they do define the necessary virtual functions specified in <strong>exception<\/strong>.<\/p>\n\n\n\n<p>Thus, you could write code such as:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  main.cpp\n\/\/  The Standard C++ Library Exceptions\n\/\/\n\/\/  Created by Bryan Higgs on 10\/8\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;exception&gt;\n\nvoid useStandardLibrary()\n{\n  \/\/ ...\n  throw std::bad_exception();\n}\n\nint main(int argc, const char * argv[]) \n{\n  try\n  {\n    useStandardLibrary();\n  }\n  catch (std::exception &amp;e)\n  {\n    std::cerr &lt;&lt; &quot;Standard Library exception &quot;\n              &lt;&lt; e.what() &lt;&lt; std::endl;\n  }\n  catch (...)\n  {\n    std::cerr &lt;&lt; &quot;Other exception encountered...&quot;\n              &lt;&lt; std::endl;\n  }\n\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  The Standard C++ Library Exceptions<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 10\/8\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;exception&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">useStandardLibrary<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">throw<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">bad_exception<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[]) <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">try<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">useStandardLibrary<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">catch<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::exception &amp;e)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cerr &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Standard Library exception &quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">              &lt;&lt; <\/span><span style=\"color: #001080\">e<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">what<\/span><span style=\"color: #000000\">() &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">catch<\/span><span style=\"color: #000000\"> (...)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cerr &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Other exception encountered...&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">              &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>&#8230; which outputs the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Standard Library exception std::bad_exception\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<div style=\"height:47px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size has-medium-font-size\"><a class=\"wp-block-button__link has-palette-color-8-color has-text-color has-link-color wp-element-button\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/exceptions-2\/summary-of-exceptions\/\">Next: Summary of Exceptions<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Standard Exceptions The Standard C++ library provides a set of exceptions that are also available for your own use. They also provide a model for how you might construct your own exception hierarchy, perhaps inheriting from a suitable standard library exception class. exception is the base class for all the Standard C++ library exceptions. It [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":938,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1997","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1997","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=1997"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1997\/revisions"}],"predecessor-version":[{"id":2026,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1997\/revisions\/2026"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/938"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=1997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}