try: except <never>:

  • Thread starter Hallvard B Furuseth
  • Start date
H

Hallvard B Furuseth

I'd like an 'except <exception which is never raised>' statement
Is there a defined way to do that, for Python 2.2 and above?
'except None:' works for now, but I don't know if that's safe:

for ex in ZeroDivisionError, None:
try:
1/0
except ex:
print "Ignored first exception."

I could just use
except ZeroDivisionError:
if not <first iteration>:
raise
print "Ignored first exception."
but the variant above gets a bit neater.
 
P

Paul Rubin

Hallvard B Furuseth said:
'except None:' works for now, but I don't know if that's safe:

for ex in ZeroDivisionError, None:
try:
1/0
except ex:
print "Ignored first exception."

class NeverRaised(Exception): pass

for ex in ZeroDivisionError, NeverRaised:
...
 
H

Hallvard B Furuseth

Paul said:
class NeverRaised(Exception): pass

for ex in ZeroDivisionError, NeverRaised:

Heh. Simple enough. Unless some obstinate person raises it anyway...
 
P

Paul Rubin

Hallvard B Furuseth said:
Heh. Simple enough. Unless some obstinate person raises it anyway...

Hmm, ok, how's this?:

def NeverRaised():
class blorp(Exception): pass
return blorp
for ex in ZeroDivisionError, NeverRaised():
...
 
D

Duncan Booth

Paul said:
Hmm, ok, how's this?:

def NeverRaised():
class blorp(Exception): pass
return blorp
for ex in ZeroDivisionError, NeverRaised():
...

Or you can create an unraisable exception:
class NeverRaised(Exception):
def __init__(self, *args):
raise RuntimeError('NeverRaised should never be raised')
try:
raise NeverRaised
except NeverRaised:
print "caught it"



Traceback (most recent call last):
File "<pyshell#47>", line 4, in __init__
raise RuntimeError('NeverRaised should never be raised')
RuntimeError: NeverRaised should never be raised
 
T

Tom Anderson

Nice.

Or you can create an unraisable exception:

class NeverRaised(Exception):
def __init__(self, *args):
raise RuntimeError('NeverRaised should never be raised')

Briliant! Although i'd be tempted to define an UnraisableExceptionError to
signal what's happened. Or ...

class ImpossibleException(Exception):
def __init__(self, *args):
raise ImpossibleException, args

Although crashing the interpreter is probably overkill.

tom
 
D

Duncan Booth

Tom said:
Briliant! Although i'd be tempted to define an
UnraisableExceptionError to signal what's happened. Or ...

class ImpossibleException(Exception):
def __init__(self, *args):
raise ImpossibleException, args

Although crashing the interpreter is probably overkill.

Crashng the interpreter would be, but what you just wrote is simply a more
obscure way of raising RuntimeError :)
.... def __init__(self, *args):
.... raise ImpossibleException, args
....Traceback (most recent call last):
File "<stdin>", line 3, in __init__
RuntimeError: maximum recursion depth exceeded
 
H

Hallvard B Furuseth

Thanks for the help.

Tom said:
Briliant! Although i'd be tempted to define an UnraisableExceptionError
to signal what's happened. Or ...

A package we are using has ProgrammingError (like AssertionError except
__debug__ doesn't disable it), and RealityError when something *really*
can't happen:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top