0

I have a requirement to execute multiple Python statements and few of them might fail during execution, even after failing I want the rest of them to be executed.

Currently, I am doing:

try: wx.StaticBox.Destroy() wx.CheckBox.Disable() wx.RadioButton.Enable() except: pass 

If any one of the statements fails, except will get executed and program exits. But what I need is even though it is failed it should run all three statements.

How can I do this in Python?

4
  • 2
    How can it possibly run all 3 if any one of them can't run because it failed? Commented Jul 1, 2015 at 6:58
  • 3
    1. Don't use bare except, at the very least use except Exception. 2. If these statements should be executed (or not) independently, put them in separate try blocks! Commented Jul 1, 2015 at 7:00
  • @TimCastelijns I guess he means they should all be attempted. Commented Jul 1, 2015 at 7:00
  • Are each of the statements (in your actual code) method calls? Commented Jul 1, 2015 at 7:03

3 Answers 3

6

Use a for loop over the methods you wish to call, eg:

for f in (wx.StaticBox.Destroy, wx.CheckBox.Disable, wx.RadioButton.Enable): try: f() except Exception: pass 

Note that we're using except Exception here - that's generally much more likely what you want than a bare except.

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

Comments

4

If an exception occurs during a try block, the rest of the block is skipped. You should use three separate try clauses for your three separate statements.

Added in response to comment:

Since you apparently want to handle many statements, you could use a wrapper method to check for exceptions:

def mytry(functionname): try: functionname() except Exception: pass 

Then call the method with the name of your function as input:

mytry(wx.StaticBox.Destroy) 

6 Comments

I just took an example as 3 statements. In real time i have more than 12 statements. 12 try except block in program wouldn't look good.
@Arunkumar In that case, use a wrapper method. See my edit, which I will make in a second.
@Arunkumar then you need "more than 12" blocks, or to refactor into a loop. You may be able to use contextlib.suppress to neaten the code if you're using Python 3.4.
mytry seems a bit limited; at the very least, allow it to take the errors to suppress as an argument (the default can still be Exception).
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx_core.py", line 16712, in getattr raise PyDeadObjectError(self.attrStr % self._name) wx._core.PyDeadObjectError: The C++ part of the TextCtrl object has been deleted, attribute access no longer allowed.
|
-1

I would recommend creating a context manager class that suppress any exception and the exceptions to be logged.

Please look at the code below. Would encourage any improvement to it.

import sys class catch_exception: def __init__(self, raising=True): self.raising = raising def __enter__(self): pass def __exit__(self, type, value, traceback): if issubclass(type, Exception): self.raising = False print ("Type: ", type, " Log me to error log file") return not self.raising def staticBox_destroy(): print("staticBox_destroy") raise TypeError("Passing through") def checkbox_disable(): print("checkbox_disable") raise ValueError("Passing through") def radioButton_enable(): print("radioButton_enable") raise ValueError("Passing through") if __name__ == "__main__": with catch_exception() as cm: staticBox_destroy() with catch_exception() as cm: checkbox_disable() with catch_exception() as cm: radioButton_enable() 

2 Comments

Oh... wow. Please immediately read stackoverflow.com/editing-help, I'm not even touching that.
I have fixed the formatting. Sorry, using stack for the first time. Thanks for the link.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.