473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Catching external program exceptions

I need to catch exceptions thrown by programs started by the os.system function,
as indicated by a non-zero return code (e.g. the mount utility). For example,
if I get the following results in a bash shell:

$mount test
mount: can't find /home/marty/test in /etc/fstab or /etc/mtab

then I want to catch the same exception from the corresponding os.system() call,
i.e. "os.system('mount test')", but it doesn't work as expected:

>>import os, sys
try: os.system('mount test')
.... except: print 'error'
....
mount: can't find /home/marty/test in /etc/fstab or /etc/mtab
256
>>>
I get the same results with popon, popen2, popen3, etc. Apparently these also
work only when the program does not generate an exception. Is there any way to
catch the return code. or if not, a workaround?
Jan 1 '08 #1
1 2402
En Tue, 01 Jan 2008 20:57:44 -0200, Marty <ma*****@earthlink.netescribi�:
I need to catch exceptions thrown by programs started by the os.system
function,
as indicated by a non-zero return code (e.g. the mount utility). For
Notice that these are two different things. You can wait until the
external program ends, and get its return code; and you can sometimes read
a message from its stderr. But none of these things by itself throws an
exception in Python.
example,
if I get the following results in a bash shell:

$mount test
mount: can't find /home/marty/test in /etc/fstab or /etc/mtab

then I want to catch the same exception from the corresponding
os.system() call,
i.e. "os.system('mount test')", but it doesn't work as expected:
Perhaps it doesn't work as *you* expected, but it does work as specified.
From the os.system description at
http://docs.python.org/lib/os-process.html
"On Unix, the return value is the exit status of the process encoded in
the format specified for wait(). [...] [That return value] is
system-dependent."
Later on the same page, describing the wait function, says that the return
value is "a 16-bit number, whose low byte is the signal number that killed
the process, and whose high byte is the exit status (if the signal number
is zero)"
So os.system does NOT raise an exception when the executed program ends
with a non-zero exit code.
I get the same results with popon, popen2, popen3, etc. Apparently
these also
work only when the program does not generate an exception.
An external program cannot generate an exception inside the Python
program. Only Python itself can do that.
Is there any way to
catch the return code. or if not, a workaround?
From the description above, you could do some math to obtain the exit code
looking at the os.system return value.
But perhaps it's easier to use subprocess.check_call, see "Convenience
functions" at http://docs.python.org/lib/module-subprocess.html

--
Gabriel Genellina

Jan 2 '08 #2

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

Similar topics

0
by: bB | last post by:
I am calling an external dll (compiled JScript) from within a try/catch/finally block in a C# program and am unable to catch exceptions being raised in the JScript dll by having a specific...
4
by: Laxmikant Rashinkar | last post by:
Hi, I am working with a program that someone else wrote. This program is full of bugs and unhandled exceptions are thrown all over the code. Instead of trying to catch the exception at each...
3
by: Manish Soni | last post by:
Hi, A module which I am calling may have a run time error. Now currently in such case user gets message box saying run time error has occured. My aim is to catch this runtime error and show a...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
0
by: Wild Wind | last post by:
Hello, I have a mixed dll which has to catch standard c++ library exceptions. Now I understand that when *any* object is thrown as an exception in managed c++, it is wrapped as a SEHException...
0
by: Epetruk | last post by:
Hello, I posted this a while back, but I have had no response yet. I have a mixed dll which has to catch standard c++ library exceptions. Now I understand that when any object is thrown as...
5
by: Simon Tamman | last post by:
I have an object named DisasterRecovery. The Ctor of this object is this: private DisasterRecovery() { Application.ThreadException+= new...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.