[Tutor] How to handle exceptions raised inside a function?

patty at cruzio.com patty at cruzio.com
Wed Dec 1 19:46:30 CET 2010


I think I understand, I will have to reread this a couple times!  But I do
consider myself a C programmer so that probably explains why I was trying
to write code that way.  And you are right 'you must inspect the global
> after each call, before making the next call'.  I *was* setting up the
function to check on and muck with this mvar before the end where I
'return' my own thing.

So for Python programming you are advising us to use one of the other 
four approaches to error handling and not try to do something like:

call custom function, something is returned to a mvar while processing
each statement, which is then examined for content and if one of three
'bad' or 'good' things are in the mvar, trying to avoid return values of
[0 1 -1 2 -2] because off-the-top-of-my-head these are meaningful in
various languages, so come up with 3 way-out-there-numbers-of-my-own, such
as your '42'example.  Then try and 'return' this from the function back to
the calling function, possibly main().  Then examine return value integer
and have ' if statements' doing something depending...

So does this mean I am thinking through this like a C programmer?

And I don't think I would be checking a variable at all using the other
four ways, I would just let an error happen and let the return value be
whatever it is and let the exception come up (or my custom exception
handler) and handle it, instead I was trying to get right in the middle of
it and force things.  Also I was trying to use the return value for
purposes other than error.  I think what I was trying to do would be like
a Case Statement
really if that were supported in Python.


 return my_special_integer_mvar

So the above will not work?  If you were to try this, do you have to
return digits?  You can't return an mvar (and hope it doesn't change on
you while going back to calling program)?

Thanks for confirming my understanding or confusion as the case may be!!

Patty


> Patty wrote:
>> This is very interesting to me - the below excerpt is something I was
>> trying to do for one of my programs and gave up on it:
>>
>>> A fifth approach, common in some other languages, is to return some
>>> arbitrary value, and set an error flag. The caller then has to write
>>> code like this:
>>>
>>> result = function(arguments)
>>> if not last_result_error:
>>>     # no error occurred
>>>     print "result is", result
>>>
>>>
>>> If you do this, I will *personally* track you down and beat you to
>>> death with a rather large fish.
>>>
>>> *wink*
>>
>> I think I was trying to do something like thius at the end of a function
>> I wrote-
>>
>> return 2  or return my_special_integer_mvar
>
> That syntax won't work. However, the basic idea is (moderately) sound:
> your function has a special value that means "something funny happened".
> Python very occasionally uses this:
>
>  >>> "hello world".find("z")  # not found
> -1
>
> which you then use like this:
>
> result = string.find(target)
> if result == -1:  # special value
>      print("not found")
> else:
>      print("found at position %d" % result)
>
> In general, this idiom is mildly disparaged in Python circles, but not
> forbidden. Exceptions are usually considered better.
>
> However, what I'm talking about is quite different. Here's how I might
> write the string find method using this (horrible) implementation:
>
>
> # Global status flag.
> find_succeeded = 0
>
> def find(string, target):
>      global find_succeeded
>      if target in string:
>          find_succeeded = 1
>          return string.find(target)
>      else:
>          find_succeeded = 0
>          # I never know what number to return...
>          return 42  # that'll do...
>
> (In low-level languages like C, the number returned on failure (where I
> choose 42) is often whatever value happens to be in some piece of memory
> -- essentially a random number.)
>
>
> result = find("hello world", "z")
> if find_succeeded == 1:
>      print("found at position %d" % result)
> else:
>      print("not found")
>
>
> This is even more inconvenient and difficult than earlier. Consider what
> happens if you want to do two or more searches. Because they all report
> their status via the one global variable, you must inspect the global
> after each call, before making the next call, or the status will be
> lost. In Python you can do this:
>
> results = [s.find(target) for s in list_of_strings]
>
> and then, later, inspect each individual result to see if it was -1 or
> not. But with the global status idiom, you can't do that, because the
> status flag is lost once you call find() again.
>
>
>
> --
> Steven
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>




More information about the Tutor mailing list