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

2013-10-22 Thread 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 su

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

2013-10-21 Thread Ben Finney
Victor Hooi writes: > Aha, good point about IOError encapsulating other things, I'll use > FileNotFoundError, and also add in some other except blocks for the > other ones. Or not; you can catch OSError, which is the parent of FileNotFoundError http://docs.python.org/3/library/exceptions.html#ex

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

2013-10-21 Thread 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.

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

2013-10-21 Thread Ben Finney
Victor Hooi writes: > try: > with open('somefile.log', 'wb' as f: > f.write("hello there") > except IOError as e: > logger.error("Uhoh, the file wasn't there"). IOError, as Steven D'Aprano points out, is not equivalent to “file not found”. Also, you're not doi

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

2013-10-21 Thread Steven D'Aprano
On Mon, 21 Oct 2013 18:43:39 -0700, Victor Hooi wrote: > 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 no

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

2013-10-21 Thread MRAB
On 22/10/2013 02:43, Victor Hooi wrote: 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 i

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

2013-10-21 Thread 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 wra