catching my own exception

Peter Otten __peter__ at web.de
Fri Jun 18 14:01:45 EDT 2010


nick wrote:

> I have a problem with catching my own exception. Here is the code:
> 
http://fly.srk.fer.hr/~nick/travapi/blame.php?repname=Travian+API&path=%2Fvillage.py&
> 
> Line 252 calls a method, which on line 207 raises a
> SomethingBeingBuiltError exception. On line 253 I catch that
> exception, but when I run that code, I get:
> 
> Traceback (most recent call last):
>   File "village.py", line 252, in <module>
>     v.upgrade_cheapest()
>   File "/media/data/home/code/travapi/village.py", line 207, in
>   upgrade_cheapest
>     raise SomethingBeingBuiltError()
> village.SomethingBeingBuiltError: 'Unable to upgrade, check if something
> is being built.'
> 
> If I change line 253 to only "except:", everything works as
> expected. How come? What am I missing? Any help would be appreciated.
> 
> P.S.: I'm aware I'm exposing my login/pass in the code. Don't care
> really, those are throwaway accounts.

You are importing your main script elswhere. Your code then 
effectively becomes

try:
    # in another module
    raise village.SomethingBuiltError 
except __main__.SomethingBeingBuiltError:
   print "caught"

i. e. you get two versions of every class that are built from the same code 
but not (recognized as) identical.

Solution: move your startup code into a separate file and have it import the 
village module.

Peter



More information about the Python-list mailing list