[Baypiggies] Python way to avoid usage of raise

Stephen Lacy slacy at slacy.com
Wed Oct 6 06:32:48 CEST 2010


If you're not allowed to raise the exception, why not just return it?

I can imagine a pattern where every function was a tuple return value (as
stated previously) where the first item is the real return value, and the
2nd item is either None or an instance of an Exception.

Then, you could wrap every function in your entire system with a decorator
that looked at these return values, and if the Exception part was non-None,
then it would return back out to the caller, possibly calling some cleanup
function that would be specified as an argument to the decorator.  I can
code up some kind of rudimentary example if you'd like.

Of course, this path gets dangerously close to "let's do exactly the same
thing as exceptions but not use the built in language features ourselves and
rewrite the whole thing from scratch so we have a maintenance nightmare".

Is it too late to quit and go work somewhere else?

Steve

On Tue, Oct 5, 2010 at 9:20 PM, Tung Wai Yip <tungwaiyip at yahoo.com> wrote:

> The recipe only collect information from the stack. It won't resume
> execution after an exception is throw.
>
> It maybe easier to use a wrapper for all function call.
>
>
>  def call(func, *args, **kwargs):
>>>>
>>> ...   try:
> ...     return func(*args,**kwargs)
> ...   except:
> ...     # auto ignore exception!
> ...     return
> ...
>
>> res = call(open,'dummy file','rb')
>>>> # look mom, no exception!
>>>>
>>>
> Wai Yip
>
>
>
>
>  On Tue, Oct 5, 2010 at 7:15 PM, Nick S Kanakakorn <bbdada at gmail.com>
>> wrote:
>>
>>> Hi,
>>> My work place has imposed a rules for no use of exception (catching is
>>> allowed).  If I have code like this
>>>
>>
>> I've seen this anti-pattern up close. There is no Pythonic answer -
>> exceptions are the answer. I've adapted this recipe[0] to get all
>> kinds of information from the call stack when something fails,
>> although it depends on the failing function to call it... Definitely a
>> pain. I feel for you. Hopefully the code is solid enough not to need
>> too much debugging.
>>
>> For the curious, the rationale I heard is that failure isn't important
>> enough to stop the execution or the minor variant, failures are
>> irrelevant to the execution.
>>
>> [0] http://code.activestate.com/recipes/52215/
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> http://mail.python.org/mailman/listinfo/baypiggies
>>
>
>
> --
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20101005/444fb126/attachment-0001.html>


More information about the Baypiggies mailing list