473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

try: except <never>:

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.

--
Hallvard
Jan 10 '06 #1
7 1359
Hallvard B Furuseth <h.**********@usit.uio.no> writes:
'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:
...
Jan 10 '06 #2
Paul Rubin writes:
Hallvard B Furuseth <h.**********@usit.uio.no> writes:
'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:


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

--
Hallvard
Jan 10 '06 #3
Hallvard B Furuseth <h.**********@usit.uio.no> writes:
class NeverRaised(Exception): pass
for ex in ZeroDivisionError, NeverRaised:


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():
...
Jan 10 '06 #4
Paul Rubin wrote:
Hallvard B Furuseth <h.**********@usit.uio.no> writes:
> class NeverRaised(Exception): pass
> for ex in ZeroDivisionError, NeverRaised:


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():
...


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
Jan 10 '06 #5
On Tue, 10 Jan 2006, Duncan Booth wrote:
Paul Rubin wrote:
Hallvard B Furuseth <h.**********@usit.uio.no> writes:
class NeverRaised(Exception): pass
for ex in ZeroDivisionError, NeverRaised:

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():
...
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

--
Like Kurosawa i make mad films; okay, i don't make films, but if i did
they'd have a samurai.
Jan 10 '06 #6
Tom Anderson wrote:
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.


Crashng the interpreter would be, but what you just wrote is simply a more
obscure way of raising RuntimeError :-)
class ImpossibleException(Exception): .... def __init__(self, *args):
.... raise ImpossibleException, args
.... raise ImpossibleException

Traceback (most recent call last):
File "<stdin>", line 3, in __init__
RuntimeError: maximum recursion depth exceeded
Jan 11 '06 #7
Thanks for the help.

Tom Anderson writes:
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 ...


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

--
Hallvard
Jan 13 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
11
by: Ted Mayett | last post by:
OK. Here is a glitch, sorry if this has been mentioned before. This is an erratic glitch. I am now up to three other people besides myself who have been able to see this glitch. It seems it...
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
7
by: Robert Allan Schwartz | last post by:
Why do I get a syntax error below? I don't see why volatile works but unsigned does not work. I'm not looking for an answer of the form, "Because the Standard says so", or "Because the C++...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
31
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I modify the current browser window?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.