[Tutor] I don't understand example 12.10.2
Jeff Shannon
jeff@ccvcorp.com
Mon May 12 13:25:01 2003
Daniel Nash wrote:
> and what is going on here?
>
> try:
> data = StringIO.StringIO()
> mimetools.decode(file, data, submsg.getencoding())
> except ValueError:
> continue
Magnus already told you about StringIO, so I won't repeat that. For the
rest of it, this fragment is hard to analyze out of context. It looks
like this must be a segment of a loop, because 'continue' is only valid
within a loop. What you have here is an exception block. Python tries
to run the code within the 'try:' segment of the block, and that's all
that runs if there's no errors detected. However, if something in that
segment has a problem, it can throw an exception. Exceptions will
normally stop your program, unless you catch them, which is what the
'except' segment is supposed to do. In this case, if there is an
exception that is of type 'ValueError', then Python will run the
specified bit of code in an attempt to let you recover from the error.
The recovery, in this case, consists only of 'continue', which
immediately returns to the top of your loop and begins the next
iteration of the loop, essentially throwing away whatever has been done
so far (since it's somehow invalid, presumably because of a bad submsg).
In other words, paraphrasing this fragment in English, it would be
something like: "Try to decode the submessage into a new StringIO
file-like object. If you run into any invalid values, then skip it and
start over again with the next submessage." (I am, of course, only
guessing that the loop iterates over submessages, since you didn't show
the beginning of the loop...)
Jeff Shannon
Technician/Programmer
Credit International