{"id":4517,"date":"2023-12-15T10:55:28","date_gmt":"2023-12-15T10:55:28","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4517"},"modified":"2024-01-06T12:38:02","modified_gmt":"2024-01-06T12:38:02","slug":"using-error-catching","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/try-error-catching\/using-error-catching","title":{"rendered":"Using error catching"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>try:\n    print(\"Doing something\")\nexcept:\n    print(\"Something went wrong\")\n\n\ntry:\n    print(\"Doing something\")\nexcept BaseException as e:\n    print(\"Something went wrong: \" + str(e))\n\n\ntry:\n    print(\"Doing something\")\nexcept:\n    print(\"Something went wrong\")\nfinally:\n    print(\"The 'try except' is finished\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Catching specific errors<\/h4>\n\n\n\n<p>See the different <a href=\"https:\/\/www.w3schools.com\/python\/python_ref_exceptions.asp\" target=\"_blank\" rel=\"noreferrer noopener\">exception types here<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    print(\"Doing something\")\nexcept NameError:\n  print(\"Variable x is not defined\")\nexcept:\n  print(\"Something else went wrong\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">If no errors occurred<\/h4>\n\n\n\n<p>You can use the else keyword to define a block of code to be executed if no errors were raised:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    print(\"Doing something\")\nexcept:\n    print(\"Something went wrong\")\nelse:\n  print(\"Nothing went wrong\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Raising an error<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    raise\n\n    raise Exception(\"Sorry, no numbers below zero\")\n\n    raise TypeError(\"Only integers are allowed\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Catching specific errors See the different exception types here If no errors occurred You can use the else keyword to define a block of code to be executed if no errors were raised: Raising an error<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[363],"tags":[],"class_list":["post-4517","post","type-post","status-publish","format-standard","hentry","category-try-error-catching"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4517"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4517\/revisions"}],"predecessor-version":[{"id":4551,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4517\/revisions\/4551"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}