{"id":1125,"date":"2010-08-09T12:05:32","date_gmt":"2010-08-09T16:05:32","guid":{"rendered":"http:\/\/sahd.lamafam.org\/?p=1125"},"modified":"2010-08-09T12:05:32","modified_gmt":"2010-08-09T16:05:32","slug":"different-python-user-exception-handling","status":"publish","type":"post","link":"https:\/\/sahd.lamafam.org\/?p=1125","title":{"rendered":"Different Python User Exception Handling"},"content":{"rendered":"<p>I came up with an improved means of handling my own exception classes.  Er, well, improved for me anyway.  More capable programmers than I probably do even more interesting stuff.<\/p>\n<p><!--more--><\/p>\n<p>My initial use of exception classes basically consisted of the following:<\/p>\n<pre><code>class myError(Exception):\n    def __init__(self, msg):\n        self.message = msg\n\n    def __str__(self)\n        return self.message\n<\/code><\/pre>\n<p>When I wanted to create an exception, I&#8217;d raise it like so:<\/p>\n<pre><code>if an_exception_should_be_raised:\n    raise myError(\"Error string\")\n<\/code><\/pre>\n<p>The error string would be something useful to identify what the problem was.  In turn, I would capture them in the usual <code>try: ... except:<\/code> statements and just print out the error and exit the program.<\/p>\n<p>In the course of modifying my program, I came across a situation where, to implement an improvement, I needed to detect a certain kind of error within the exception class.  For that specific case, the program would behave a little differently, while for all other cases, I wanted the program to do the usual abort with a message.<\/p>\n<p>In thinking over how to approach it, I realized something about the <code>except<\/code> clauses I was using.  The exception returns an instance of the error object:<\/p>\n<pre><code>try:\n    ... some code ...\nexcept myError, err:\n    ... exception handling ...\n<\/code><\/pre>\n<p>The <code>err<\/code> is an instance of my exception class <code>myError<\/code>.  I then realized that I could create an error code that would allow me to differentiate the type of exception being raised.  So after a little experimentation and refinement, my new exception class looks like this:<\/p>\n<pre><code>class myError(Exception):\n    ERRCODE1 = 0\n    ERRCODE2 = 1\n    ERRCODE3 = 3\n\n    ERR_STRINGS = [\n                    'Error string 1',\n                    'Error string 2',\n                    'Error string 3'\n                  ]\n\n    def __init__(self, err_code):\n        self.message = \"myError: %s\\n\" % self.ERR_STRINGS[err_code]\n        self.code = err_code\n\n    def __str__(self):\n        return self.message\n<\/code><\/pre>\n<p>So now, I raise an exception like so:<\/p>\n<pre><code>if an_exception_should_be_raise:\n    raise myError(myError.ERRCODE1)\n<\/code><\/pre>\n<p>and I can now determine the specific exception with an <code>if<\/code> statement under the <code>except<\/code>:<\/p>\n<pre><code>try:\n    ...some code...\nexcept myError, err:\n    if err.code == myError.ERRCODE1:\n       ... other code ...\n    else:\n       ... print and exit ...\n<\/code><\/pre>\n<p>I gained several advantages with the new exception class.  First, the error strings are now all kept with the exception class, so I don&#8217;t have to continually duplicate the string throughout my code every time I raise the same exception.  Second, I achieved my objective where I can detect a certain type of error condition within my exception class so I can handle it a little differently.  For me, this is superior to creating different exception classes and coding to catch all the different ones.  I prefer a general class for my module so I only have to worry about catching one kind of exception.  Third, the error code gives me a more terse means of coding what happened, so my code where the error is created has become more readable without long strings being passed to the exception.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I came up with an improved means of handling my own exception classes. Er, well, improved for me anyway. More capable programmers than I probably do even more interesting stuff.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1125","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/posts\/1125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1125"}],"version-history":[{"count":0,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/posts\/1125\/revisions"}],"wp:attachment":[{"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}