2

I'm writing a program similar to a diagnostic program , that runs a test and then based on that do more tests, so most of these are done inside `try,except and there are quite a lot of them. Is there any other way to achieve this but reduce the number of try except?

Here is a sample code.

try: treeinfo = subprocess.check_output(['C:\Python27\Scripts\scons.bat','-f' ,'scons_default.py' ,'--tree=all']) print "\n" print "Your machine type is ",platform.machine() print "Compiling using default compiler\n" print treeinfo except subprocess.CalledProcessError as e: print "ERROR\n" try: with open ('helloworld.exe')as f: subprocess.call('helloworld.exe') print"Build success" log64 = subprocess.check_output(["dumpbin", "/HEADERS", "helloworld.exe"]) if arch64 in log64: print "Architecture of the compiled file is 64-bit " elif arch32 in log64: print "Architecture of the compiled file is 32-bit" except IOError as e: print "Build failed\n" print "\n" 

The same code above (with different file names) repeat and I know that it's not a good practice to do that. I'm pretty new to python and googling didn't give any helpful results.

5
  • 1
    You don't need to open helloworld.exe in order to execute it. Also, do you want the program to continue execution when it has encountered an error? Commented Jun 25, 2012 at 9:15
  • I'm not opening the program, I'm checking to see if that program exists, if it does then open it , yes I want the program to execute the next test even if this one fails and this ones prints the result at the very end, such as which one failed and succeed. Commented Jun 25, 2012 at 9:31
  • You are opening the program. There's no need; subprocess.call will throw an exception if the program doesn't exist. Commented Jun 25, 2012 at 10:16
  • so instead of try: with open ('helloworld.exe')as f: just go try: subprocess.call('helloworld.exe') ? Commented Jun 25, 2012 at 10:22
  • Yes. Python library functions will always fail gracefully, it's part of the language's design philosophy. Commented Jun 25, 2012 at 10:23

1 Answer 1

4

You can split your logic into separate functions and call them one by one in a try block:

def a(): treeinfo = subprocess.check_output(['C:\Python27\Scripts\scons.bat','-f' ,'scons_default.py' ,'--tree=all']) print "\n" print "Your machine type is ",platform.machine() print "Compiling using default compiler\n" print treeinfo def b(): subprocess.call('helloworld.exe') print"Build success" def c(): log64 = subprocess.check_output(["dumpbin", "/HEADERS", "helloworld.exe"]) if arch64 in log64: print "Architecture of the compiled file is 64-bit " elif arch32 in log64: print "Architecture of the compiled file is 32-bit" def try_these(funs, catch): for fun in funs: try: fun() except catch: print 'ERROR!' try_these([a, b, c], catch=(IOError, OSError)) 

where "catch" a tuple of exceptions you want to handle.

Sign up to request clarification or add additional context in comments.

2 Comments

There are 14 try catch exceptions at the moment( I know, pretty bad!), I'll have a go at this.
the syntax for the except clause should be "except (OSError, IOError), e", else it will only catch OSError instances and bind them to the name "IOError". Also, it might be better to at least print the error message - and possibly to not try going further if any of the steps is dependent on the results of the previous steps (and if it's the case, the best exception handling scheme is no exception handling at all).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.