Using "with" context handler, and catching specific exception?

V

Victor Hooi

Hi,

I suspect I'm holding

How should I use the "with" context handler as well as handling specific exceptions?

For example, for a file:

with open('somefile.log', 'wb') as f:
f.write("hello there")

How could I specifically catch IOError in the above, and handle that? Should I wrap the whole thing in a try-except block?

(For example, if I wanted to try a different location, or if I wanted to print a specific error message to the logfile).

try:
with open('somefile.log', 'wb' as f:
f.write("hello there")
except IOError as e:
logger.error("Uhoh, the file wasn't there").

Cheers,
Victor
 
M

MRAB

Hi,

I suspect I'm holding

How should I use the "with" context handler as well as handling specific exceptions?

For example, for a file:

with open('somefile.log', 'wb') as f:
f.write("hello there")

How could I specifically catch IOError in the above, and handle that? Should I wrap the whole thing in a try-except block?
Yes, it's as simple as that.
 
S

Steven D'Aprano

try:
with open('somefile.log', 'wb' as f:
f.write("hello there")
except IOError as e:
logger.error("Uhoh, the file wasn't there").

I hope that this isn't what you are actually doing. IOError is not just
for "File Not Found".


py> open("/home")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 21] Is a directory: '/home'

py> open("/root/foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 13] Permission denied: '/root/foo'
 
V

Victor Hooi

Hi,

Thanks for the replies =).

Aha, good point about IOError encapsulating other things, I'll use FileNotFoundError, and also add in some other except blocks for the other ones.

And yes, I didn't use the exception object in my sample - I just sort. I'd probably be doing something like this.

logger.error("Some error message - %s" % e)

So is the consensus then that I should wrap the "with" in a try-except block?

try:
with open('somefile.log', 'wb') as f:
f.write("hello there")
except FileNotFoundError as e:
logger.error("Uhoh, the file wasn't there - %s" % e)

Cheers,
Victor
 
V

Victor Hooi

Hi,

I'm actually on Python 2.7, so we don't have access to any of those nice new exceptions in Python 3.3 =:)

http://docs.python.org/2.7/library/exceptions.html#exception-hierarchy

@Ben - Good point about just catching the more general exception, and just printing out the string message.

I suppose in most cases, we won't be doing anything special for the different types (e.g. file not found, permission error, is a directory etc.) - it'll just be going into logs.

Is there anything wrong with me just catching "Exception" in this case of opening a file, and printing the message from there?

Cheers,
Victor
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top