{"id":20345804,"url":"https://github.com/izzypt/cpp-module-05","last_synced_at":"2026-04-17T22:04:04.616Z","repository":{"id":210286293,"uuid":"726196860","full_name":"izzypt/CPP-Module-05","owner":"izzypt","description":"Repetition and Exceptions","archived":false,"fork":false,"pushed_at":"2023-12-11T17:06:08.000Z","size":47,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-04T15:48:02.427Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/izzypt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-01T18:38:41.000Z","updated_at":"2023-12-01T20:41:13.000Z","dependencies_parsed_at":"2023-12-11T18:27:07.605Z","dependency_job_id":"8218fade-1204-4b49-a9ee-65fc19fad88b","html_url":"https://github.com/izzypt/CPP-Module-05","commit_stats":null,"previous_names":["izzypt/cpp-module-05"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/izzypt/CPP-Module-05","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-05","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-05/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-05/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-05/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izzypt","download_url":"https://codeload.github.com/izzypt/CPP-Module-05/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-05/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31947762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-17T17:29:20.459Z","status":"ssl_error","status_checked_at":"2026-04-17T17:28:47.801Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T22:09:50.117Z","updated_at":"2026-04-17T22:04:04.553Z","avatar_url":"https://github.com/izzypt.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CPP-Module-05\n\nThis module is designed to help you understand Try/Catch and Exceptions in CPP. \n\n# C++ Exceptions\n\nC++ includes a new, improved mechanism for capturing and handling errors called **exceptions**.\n\nAn **exception** is “a case in which a rule or principle does not apply.” \n\nException is also defined as an objection to something. \n\nEither definition works: An exception is an unexpected (and presumably objectionable) condition that occurs during the execution of the program.\n\n\u003chr\u003e\n\nWhen executing C++ code, different errors can occur: \n\n- coding errors made by the programmer, \n- errors due to wrong input,\n- or other unforeseeable things.\n\nWhen an error occurs, C++ will normally stop and generate an error message. \n\nThe technical term for this is: C++ will throw an exception (throw an error).\n\n# Exception handling in C++ \n\nConsists of three keywords: ```try```, ```throw``` and ```catch```:\n\nThe ```try``` statement allows you to define a block of code to be tested for errors while it is being executed.\n\nThe ```throw``` keyword throws an exception when a problem is detected, which lets us create a custom error.\n\nThe ```catch``` statement allows you to define a block of code to be executed, if an error occurs in the try block.\n\n```cpp\ntry {\n  // Block of code to try\n  throw exception; // Throw an exception when a problem arise\n}\ncatch () {\n  // Block of code to handle errors\n} \n```\nAn exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block.\n\n# Creating custom exceptions\n\n\nIn C++, you can create custom exceptions by deriving a new class from the standard exception class (std::exception) or one of its derived classes. \n\nHere's a simple example:\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstdexcept\u003e\n\n// Custom exception class derived from std::exception\nclass MyException : public std::exception\n{\n  public:\n      // Constructor with a custom error message\n      MyException(const char* message) : message_(message) {}\n  \n      // Override the what() function to provide the error message\n      const char* what() const noexcept override {\n          /* c_str() is used to obtain a pointer to the internal character array of the std::string.\n             This is often useful when you need to pass the content of a C++ string to a function or API that expects a null-terminated C-style string (const char*) */\n          return message_.c_str();\n      }\n  \n  private:\n      std::string message_;\n  };\n  \n  int main() {\n      try {\n          // Throw the custom exception with a specific message\n          throw MyException(\"This is a custom exception\");\n      } catch (const MyException\u0026 e) {\n          // Catch the custom exception and print the error message\n          std::cerr \u003c\u003c \"Caught custom exception: \" \u003c\u003c e.what() \u003c\u003c std::endl;\n      } catch (const std::exception\u0026 e) {\n          // Catch other exceptions derived from std::exception\n          std::cerr \u003c\u003c \"Caught standard exception: \" \u003c\u003c e.what() \u003c\u003c std::endl;\n      }\n  \n      return 0;\n}\n```\n- In this example, the MyException class is derived from std::exception, and it has a constructor that takes a custom error message as a parameter.\n- The what() function is overridden to return the custom error message.\n- In the main function, we throw an instance of MyException with a specific message, and then we catch it in the catch block. Note that catching by reference (const MyException\u0026 e) is recommended to avoid slicing the object.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp-module-05","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizzypt%2Fcpp-module-05","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp-module-05/lists"}