Re: Python Exceptions

Jaap Vermeulen (jaap@sequent.com)
Mon, 16 Jan 95 22:58:27 GMT

>Why do python exceptions have to be strings, couldn't they be a object
that
>has a __str__ method defined returning the string wanted. If they could
>be we could throw generic objects around that can contain other actions.

Interesting suggestion.

>class Exception :
>
> def __init__(s, exception, where = None) :
> s.exception = exception
> s.where = where
>
> def __str__(s) :
> return s.exception
>
> def someAction(s) :
> ..
>
>...
>
> raise Exception(ZeroDiv, cellRef)
>
>...
>
> try : x() except ZeroDiv :
> sys.exc_type.someAction()

I would imagine that this would work slightly differently. First you
create your generic exception type. E.g.:

MyZeroDiv = Exception(...)

next you would be able to do:

MyZeroDiv.raise(where, ...)

and catch it:

try: x() except MyZeroDiv:
MyZeroDiv.someAction()

-Jaap-