Method Call in Exception

M

mwt

In my latest attempt at some Python code, I've been tempted to write
something in the form of:

try:
[...] #doing some internet stuff
except IOError:
alternate_method_that_doesnt_need_internet()

This works when I try it, but I feel vaguely uneasy about putting
method calls in exception blocks. So tell me, Brave Pythoneers, is this
evil sorcery that I will end up regretting, or is it just plain good
ol' Python magic?
 
F

Felipe Almeida Lessa

Em Qua, 2006-04-19 às 16:54 -0700, mwt escreveu:
This works when I try it, but I feel vaguely uneasy about putting
method calls in exception blocks.

What do you put in exception blocks?!

So tell me, Brave Pythoneers, is this
evil sorcery that I will end up regretting, or is it just plain good
ol' Python magic?

IMHO, the exception block in Python is used a lot in places where you
could use an if-then-else, like your example that could be written as

if internet_available():
[...] #doing some internet stuff
else:
alternate_method_that_doesnt_need_internet()

So yes, I think there's no problem there.
 
M

mwt

Felipe said:
Em Qua, 2006-04-19 às 16:54 -0700, mwt escreveu:

What do you put in exception blocks?!

Usually I just print an error message.
So tell me, Brave Pythoneers, is this
evil sorcery that I will end up regretting, or is it just plain good
ol' Python magic?

IMHO, the exception block in Python is used a lot in places where you
could use an if-then-else, like your example that could be written as

if internet_available():
[...] #doing some internet stuff
else:
alternate_method_that_doesnt_need_internet()

So yes, I think there's no problem there.

Normally I don't like to use exception blocks to do condition-statement
stuff. At least that is how my Java training has programmed me. But in
this case, the condition is whether the internet connection is working
or not, and I don't see any other way to do it. And furthermore, I
don't know if the exceptions-as-logic is a no-no in Python. Hence the
question.
 
S

Serge Orlov

Felipe said:
Em Qua, 2006-04-19 às 16:54 -0700, mwt escreveu:
This works when I try it, but I feel vaguely uneasy about putting
method calls in exception blocks.

What do you put in exception blocks?!

So tell me, Brave Pythoneers, is this
evil sorcery that I will end up regretting, or is it just plain good
ol' Python magic?

IMHO, the exception block in Python is used a lot in places where you
could use an if-then-else, like your example that could be written as

if internet_available():
[...] #doing some internet stuff
else:
alternate_method_that_doesnt_need_internet()

So yes, I think there's no problem there.

What if the service is overloaded and always times out? For end user
it's basically the same as there is no internet access. I routinely use
the following pattern:

unrecoverable = MemoryError, IOError
try:
do_your_best()
except unrecoverable, e:
util.help_on_error(e)
sys.exit(1)

def do_your_best():
recoverable = IOError, socket.error, SomeOtherError
try:
do_something()
except recoverable, e:
do_something_else()
 
C

Carl Banks

mwt said:
In my latest attempt at some Python code, I've been tempted to write
something in the form of:

try:
[...] #doing some internet stuff
except IOError:
alternate_method_that_doesnt_need_internet()

This works when I try it, but I feel vaguely uneasy about putting
method calls in exception blocks. So tell me, Brave Pythoneers, is this
evil sorcery that I will end up regretting, or is it just plain good
ol' Python magic?

It's ok. In fact a lot of Pythonistas recommend this way over the
alternative, even when you don't have to. For example, a lot of people
recommend this:

try:
name = record.name
except AttributeError:
name = "Freddy"

instead of this:

if hasattr(record,"name"):
name = record.name
else:
name = "Freddy"

I prefer the latter most of the time; however, sometimes (as it appears
in your case) simply trying it and doing something else if it raises an
exception is easier, more straightforward, and more robust than
explicitly testing the condition. One thing to consider is whether
something you don't expect could throw the same exception; might have
to disambiguate them somehow in the except block.

Language-wise, I doubt there are any serious gotchas whem running
regular (as opposed to error handling) code in an except block. So go
right ahead; no need to feel guilty about it.


Carl Banks
 
D

Duncan Booth

Carl said:
In fact a lot of Pythonistas recommend this way over the
alternative, even when you don't have to. For example, a lot of people
recommend this:

try:
name = record.name
except AttributeError:
name = "Freddy"

instead of this:

if hasattr(record,"name"):
name = record.name
else:
name = "Freddy"

In this specific case, either of these would be better written as:

name = getattr(record, 'name', 'Freddy')
 
B

bruno at modulix

mwt wrote:
(snip)
Whatever fits the specific case...

(snip)
Normally I don't like to use exception blocks to do condition-statement
stuff. At least that is how my Java training has programmed me.
http://dirtsimple.org/2004/12/python-is-not-java.html

But in
this case, the condition is whether the internet connection is working
or not, and I don't see any other way to do it.

As Felipe wrote, you could also use a if/else.

And furthermore, I
don't know if the exceptions-as-logic is a no-no in Python.

It's not.

The main guideline here is that a else/if has a constant cost - the test
is always executed -, while a try/except only adds overhead if it's
fired. So - if you leave out purely stylistic or religious
considerations - the 'good' choice depends mostly on the 'cost of the
test'/'cost of the exception' ratio and the 'have connection'/'no
connection' ratio.
 
B

bwaha

mwt said:
In my latest attempt at some Python code, I've been tempted to write
something in the form of:

try:
[...] #doing some internet stuff
except IOError:
alternate_method_that_doesnt_need_internet()
I'm only a hack at programming but where I find try: blocks useful is where
I have multiple statements within the try: block, any one or more of which
can produce an exception. I just want to trap a failure and get out of
there. This happens to me for any number of reasons when I drive other apps
through the com interface. (In my job I just write simple apps to make my
job easier, not bomb-proof customer-focused code). So in your case perhaps
you have open connection, find site, get data within the try block, and exit
gracefully if any one fails. How you use it depends on what level of detail
you need.

bwaha
 
K

Kent Johnson

Carl said:
mwt said:
In my latest attempt at some Python code, I've been tempted to write
something in the form of:

try:
[...] #doing some internet stuff
except IOError:
alternate_method_that_doesnt_need_internet()

This works when I try it, but I feel vaguely uneasy about putting
method calls in exception blocks. So tell me, Brave Pythoneers, is this
evil sorcery that I will end up regretting, or is it just plain good
ol' Python magic?

It's ok. In fact a lot of Pythonistas recommend this way over the
alternative, even when you don't have to. For example, a lot of people
recommend this:

try:
name = record.name
except AttributeError:
name = "Freddy"

instead of this:

if hasattr(record,"name"):
name = record.name
else:
name = "Freddy"

or maybe
name = getattr(record, 'name', 'Freddy')

Kent
 

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
474,294
Messages
2,571,508
Members
48,193
Latest member
DannyRober

Latest Threads

Top