Tuesday 15 January 2013

python - Define a custom exception in scriptA that can be caught by scriptB, which has imported scriptA -



python - Define a custom exception in scriptA that can be caught by scriptB, which has imported scriptA -

if want define custom exception in scripta can caught scriptb, has imported scripta, best practice?

right now, i'm defining exception in scripta, adding __builtins__ module:

# scripta class someerror(exception): pass globals()['__builtins__'][someerror.__name__] = someerror raise someerror('error scripta')

then in scriptb, error shows up:

# scriptb import scripta scripta.someerror: error scripta

or can handle exception in scriptb:

# scriptb try: import scripta except someerror, e: print e error scripta

i have been using function add together custom errors __builtins__ module multiple custom errors, imagine there improve way of doing this?

any help appreciated.

python error-handling

No comments:

Post a Comment