Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use else in try/except construct in Python?

I am learning Python and have stumbled upon a concept I can't readily digest: the optional else block within the try construct.

According to the documentation:

The try ... except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.

What I am confused about is why have the code that must be executed if the try clause does not raise an exception within the try construct -- why not simply have it follow the try/except at the same indentation level? I think it would simplify the options for exception handling. Or another way to ask would be what the code that is in the else block would do that would not be done if it were simply following the try statement, independent of it. Maybe I am missing something, do enlighten me.

This question is somewhat similar to this one but I could not find there what I am looking for.

like image 586
amphibient Avatar asked Aug 22 '13 17:08

amphibient


People also ask

What is the purpose of the else clause in a try except construct?

The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.

Do you need else in TRY except?

It runs when there is no exception but before the finally-clause. That is its primary purpose. Without the else-clause, the only option to run additional code before finalization would be the clumsy practice of adding the code to the try-clause.

What is the purpose of ELSE clause in the try statement?

In Python, variable scopes are established only by modules, functions and comprehensions, not control structures. The else clause lets you write code that only makes sense if an exception wasn't thrown; the except clause can simply pass. If you put the logic in the try block, you risk silently hiding bugs in your code.

Can else be used with except in Python?

Use the Python try... except...else statement provides you with a way to control the flow of the program in case of exceptions. The else clause executes if no exception occurs in the try clause. If so, the else clause executes after the try clause and before the finally clause.


2 Answers

The else block is only executed if the code in the try doesn't raise an exception; if you put the code outside of the else block, it'd happen regardless of exceptions. Also, it happens before the finally, which is generally important.

This is generally useful when you have a brief setup or verification section that may error, followed by a block where you use the resources you set up in which you don't want to hide errors. You can't put the code in the try because errors may go to except clauses when you want them to propagate. You can't put it outside of the construct, because the resources definitely aren't available there, either because setup failed or because the finally tore everything down. Thus, you have an else block.

like image 81
user2357112 supports Monica Avatar answered Oct 23 '22 18:10

user2357112 supports Monica


One use case can be to prevent users from defining a flag variable to check whether any exception was raised or not(as we do in for-else loop).

A simple example:

lis = range(100)
ind = 50
try:
    lis[ind]
except:
    pass
else:
    #Run this statement only if the exception was not raised
    print "The index was okay:",ind 

ind = 101

try:
    lis[ind]
except:
    pass
print "The index was okay:",ind  # this gets executes regardless of the exception

# This one is similar to the first example, but a `flag` variable
# is required to check whether the exception was raised or not.

ind = 10
try:
    print lis[ind]
    flag = True
except:
    pass

if flag:
    print "The index was okay:",ind

Output:

The index was okay: 50
The index was okay: 101
The index was okay: 10
like image 35
Ashwini Chaudhary Avatar answered Oct 23 '22 19:10

Ashwini Chaudhary