Raising a specific OSError

David Hirschfield davidh at ilm.com
Fri Apr 21 18:57:09 EDT 2006


I wasn't clear enough in my original post.

I know how to raise a basic OSError or IOError, but what if I want to 
raise specifically an "OSError: [Errno 2] No such file or directory"?
Somehow it must be possible to raise the error with the correct 
information to bring up the standard message, but where do I find the 
right values to give?

Thanks,
-Dave


alisonken1 wrote:
> To raise a specific error, just find the error that you want to raise,
> then give the error a text string to print: ex.
>
> raise IOError("This raises an IO error")
>
> On the stderr output, when the routine hits this line, you will get:
>
>   
>>>> raise IOError("This raises an IOError")
>>>>         
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> IOError: This raises an IOError
>
>   
>
> Just be sure of the error that you want to raise, since some of them
> will do stuff like closing open file descriptors as well.
>
>   

-- 
Presenting:
mediocre nebula.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060421/d50b09df/attachment.html>


More information about the Python-list mailing list