-4

I've been reading through https://wiki.python.org/moin/HandlingExceptions and https://docs.python.org/2/tutorial/errors.html, but I'm not completely clear on the following point:

Lets say I have:

b = true

try:
  statement1
  statement2
  statement3
except: 
  b = false

I would like variable bool to remain true if the entire codeblock is executed without exception ( I don't want to classify the errors ) otherwise make it false. Will the code above do this?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • possible duplicate of [Python try-else](http://stackoverflow.com/questions/855759/python-try-else) – vaultah May 29 '14 at 17:18
  • 1
    Yes, the code you have will do that. But don't use bare `except:` or call a variable `bool`. – Wooble May 29 '14 at 17:21
  • `'true' != 'True'`, and you shouldn't call your variable `bool`, as it will shadow the built-in. Also, it is likely that whatever you're doing with the flag could be achieved within the `try: except: else: finally:` structure itself. But the key question is - **why are you asking?** Haven't you tested it?! – jonrsharpe May 29 '14 at 17:21
  • 1
    Why would you waste so much time posting to Stack Overflow when you could just *run* the example you have here? – Jonathon Reinhart May 29 '14 at 17:23
  • jonrsharpe - how could this be done using the try: except: else: finally: structure itself? Wooble - How can i do this without except: ? – user1592380 May 29 '14 at 17:41

2 Answers2

3

The code you have will do that because only if the except is triggered, will bool be reassigned:

bool = true

try:
  statement1
  statement2
  statement3
except: #Only enters if there is an exception
  bool = false 

Example 1:

>>> bool = True
>>> try:
...     raise ValueError
... except:
...     bool = False
... 
>>> bool
False

In example 1, bool is modified only because there was an error (the raise raised an exception).

Example 2:

>>> bool = True
>>> try:
...     var = 0
... except:
...     bool = False
... 
>>> bool
True
>>> 

In example 2, the variable is not changed because there was no exception, so there was no reason to enter the except.

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
-1

You would use the else clause which only executes if an exception wasn't raised.

try:
    ...
except ...:
    flag = False
else:
    flag = True

Please note that generally, it is not a good idea to have bare exception handlers which completely suppress the exception as you're doing here. If you want to log the exception and re-raise it, that's a slightly different story.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Why is the `else` necessary? – juanchopanza May 29 '14 at 17:21
  • @juanchopanza -- It's not really in this case. Here you could put the `flag = True` before the `try` block, or before `except` since it won't ever raise an exception. However, in the case that the extra stuff you want to do relies on the first part of the try block having executed and if it could also raise an exception that you don't want to handle here, you need `else`. – mgilson May 29 '14 at 17:23
  • 1
    I know, but it might be confusing to OP, since in their code there is no need for the `else`. Given the question asked, your answer looks confusing. – juanchopanza May 29 '14 at 17:26
  • @juanchopanza, good point. There seems to be no point in using the `else`, the code will enter the `except` only if there is an exception. – A.J. Uppal May 29 '14 at 17:30
  • But why does it hurt to learn the appropriate way to do something? maybe next time OP wants to do this, he/she will be using an expression which could raise an exception, or will need `else` for some other reason. `else` isn't confusing -- I explained it in 1 line in this answer. – mgilson May 29 '14 at 18:02