{"id":5735,"date":"2017-12-31T16:43:57","date_gmt":"2017-12-31T16:43:57","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5735"},"modified":"2017-12-31T16:43:57","modified_gmt":"2017-12-31T16:43:57","slug":"python-exception-handling","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/python-exception-handling\/","title":{"rendered":"Python exception handling"},"content":{"rendered":"<p>Python includes a bunch of <a href=\"https:\/\/docs.python.org\/2\/library\/exceptions.html\" rel=\"noopener\" target=\"_blank\">built-in exception classes<\/a> and we can easily create our own by creating a class which derives from Exception.<\/p>\n<p>For example, here&#8217;s a MethodNotImplementedException type<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass MethodNotImplementedException(Exception):\r\n    def __init__(self, *args, **kwargs):\r\n        pass\r\n\r\n    def __str__(self):\r\n        return &quot;Method not implemented&quot;\r\n<\/pre>\n<p>In this example I&#8217;ve overridden the <em>__str__<\/em> method to return a simple string when we print the exception to stdout.<\/p>\n<p>We need not create specific exception types but obviously this allows us to also catch specific exception types.<\/p>\n<p>Here&#8217;s an example of some code that raises (throws) an exception and the code to catch and handle an exception<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass Animal:\r\n    def name(self):\r\n        raise MethodNotImplementedException()\r\n\r\n\r\na = Animal()\r\ntry:\r\n    a.name()\r\nexcept MethodNotImplementedException as e:\r\n    print(e)\r\nexcept Exception as e:\r\n    raise\r\nfinally:\r\n    print(&quot;finally called&quot;)\r\n<\/pre>\n<p>We create a try block and exceptions are caught using the <em>except<\/em> statement. As can be seen we can catch and filter each exception type we want to handle and include a <em>finally<\/em> block as required. <\/p>\n<p>To rethrow and exception we simple call the <em>raise<\/em> method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python includes a bunch of built-in exception classes and we can easily create our own by creating a class which derives from Exception. For example, here&#8217;s a MethodNotImplementedException type class MethodNotImplementedException(Exception): def __init__(self, *args, **kwargs): pass def __str__(self): return &quot;Method not implemented&quot; In this example I&#8217;ve overridden the __str__ method to return a simple string [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[195],"tags":[],"class_list":["post-5735","post","type-post","status-publish","format-standard","hentry","category-python"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5735"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5735\/revisions"}],"predecessor-version":[{"id":5738,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5735\/revisions\/5738"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}