473,786 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Raising a specific OSError

I know this should be obvious, but how does one raise a specific type of
OSError?
When I attempt to perform a file operation on a non-existent file, I get
an OSError: [Errno 2], but what if I want to raise one of those myself?

Thanks in advance,
-Dave

--
Presenting:
mediocre nebula.

Apr 21 '06 #1
4 10315

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.

Apr 21 '06 #2
I do not see the point in doing so (why not just copy+paste that
string?), but the errno (specifically ENOENT) corresponds to the
POSIX.1 error number, and the string "No such file or directory" is
done in C via strerror(ENOENT ); (check errno(3) and strerror(3)).

I doubt there is something that does this in the standard library
(just checked, there's an errno module, but it is quite sparse), but a
simple C extension would be trivial to write.

However, the best way is just to copy and paste that text into your
program, I mean, why not?

raise OSError("[Errno 2] No such file or directory")

On 4/21/06, David Hirschfield <da****@ilm.com > wrote:
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.
--
http://mail.python.org/mailman/listinfo/python-list

--
Kelvie
Apr 22 '06 #3
Looking at the Python docs.. I found this:
http://docs.python.org/ext/errors.html

"""
Another useful function is PyErr_SetFromEr rno(), which only takes an
exception argument and constructs the associated value by inspection
of the global variable errno. The most general function is
PyErr_SetObject (), which takes two object arguments, the exception and
its associated value. You don't need to Py_INCREF() the objects passed
to any of these functions.
"""

So, in a C extension, to raise a a specific OSError...

errno = ENOENT;
PyErr_SetFromEr rno(PyExc_OSErr or);

should work...

On 4/21/06, Kelvie Wong <ke****@ieee.or g> wrote:
I do not see the point in doing so (why not just copy+paste that
string?), but the errno (specifically ENOENT) corresponds to the
POSIX.1 error number, and the string "No such file or directory" is
done in C via strerror(ENOENT ); (check errno(3) and strerror(3)).

I doubt there is something that does this in the standard library
(just checked, there's an errno module, but it is quite sparse), but a
simple C extension would be trivial to write.

However, the best way is just to copy and paste that text into your
program, I mean, why not?

raise OSError("[Errno 2] No such file or directory")

On 4/21/06, David Hirschfield <da****@ilm.com > wrote:
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.
--
http://mail.python.org/mailman/listinfo/python-list

--
Kelvie

--
Kelvie
Apr 22 '06 #4
In article <ma************ *************** ************@py thon.org>,
David Hirschfield <da****@ilm.com > wrote:
When I attempt to perform a file operation on a non-existent file, I get
an OSError: [Errno 2], but what if I want to raise one of those myself?


raise OSError(2, "No such file or directory")
Apr 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1127
by: John J. Lee | last post by:
Today, I wanted to do something like this: def nr_files(dirName) try: return len(os.listdir(dirName)) except OSError, e: if e.errno == errno.ENOENT: return 0 else: raise
1
8261
by: Mark Harrison | last post by:
Can somebody loan me a clue as to how to check the errno on an OSError, as described below? try: os.makedirs(d) except OSError: # if errno == 17 then silently ignore the error because the # other process won the race condition and created the directory # otherwise, allow the exception to percolate up the call stack # and be caught by the standard error reporter
7
2444
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin Manager. Your main application then hooks into the Plugin Manager. What I'm trying to figure out is how to impliment some form of raising
4
1598
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written the way my component raises events to follow the Framework Design Guides verbatim; ie (object sender, EventArgs (or some subclass there of) e). The thing that was not explained is why should I need to cast the sender to 'object' when I know...
11
7873
by: Alec Wysoker | last post by:
Using Python 2.3.5 on Windows XP, I occasionally get OSError: Permission denied when calling os.remove(). This can occur with a file that is not used by any other process on the machine, and is created by the python.exe invocation that is trying to delete it. It can happen with various pieces of my system - almost anywhere I try to delete a file. I have assumed that the problem is that I was holding on to a handle to the file that I...
0
3976
by: Joram Agten | last post by:
Please put me in CC When running the following program I get frequent errors like this one Exception in thread Thread-4: Traceback (most recent call last): File "C:\Python24\lib\threading.py", line 442, in __bootstrap self.run() File "os.remove.py", line 25, in run os.remove(filename)
1
6828
by: Lingo | last post by:
Hi all, I have a problem, i am running a batch file which will connect to oracle database via sqlplus. If i set whenever Oserror Exit and then try to connect it fails with the error message 'o/s no such file or directory' if i dont set , i am able to connect. Why does setting Whenever osError Exit create a problem
4
3254
by: python | last post by:
Bad file names, i.e. filenames the OS considers illegal, will cause functions in the os.path module to raise an error. Example: import os.path print os.path.getsize( 'c:/pytest/*.py' ) On Windows XP using Python 2.5.2 I get the following traceback:
26
1675
by: buu | last post by:
So, let's say that a user enters an event name in text box, and would like to raise it.. is it possible to call events by their 'name'? Is it possible to enumerate events inside app. and to check their names?
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.