Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6120383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:39:44+00:00 2026-05-23T15:39:44+00:00

Why is else clause needed for try statement in python ? Taking it forward:

  • 0

Why is else clause needed for try statement in python ?

Taking it forward:

try:
  f = open('foo', 'r')
except IOError as e:
  error_log.write('Unable to open foo : %s\n' % e)
else:
  data = f.read()
  f.close()

It occurs to me that the corner case solved by else clause still can be avoided by a nested try...except avoiding the need of else? :

try:
  f = open('foo', 'r')
  try:
    data = f.read()
    f.close()
  except:
    pass 
except IOError as e:
  error_log.write('Unable to open foo : %s\n' % e)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:39:44+00:00Added an answer on May 23, 2026 at 3:39 pm

    try..except..else may not be needed, but it can be nice. In this case, the try..except..else form is distinctly nicer, in my opinion.

    Just because you can do without an element of syntax, doesn’t make it useless. Decorator syntax is purely syntax sugar (the most obvious example, I think), for loops are just glorified while loops, et cetera. There’s a good place for try..except..else and I would say this is one such place.

    Besides, those two blocks of code are far from equivalent. If f.read() raises an exception (disk read error, data corruption inside the file or some other such problem), the first will raise an exception as it should but the second will lose it and think that everything has worked. For myself, I would prefer something more along these lines, which is shorter and still easier to understand:

    try:
        with open('foo', 'r') as f:
            data = f.read()
    except IOError as e:
        error_log.write('Unable to open foo : %s\n' % e)
    

    (This assumes that you want to catch errors in file.read and file.close. And I can’t really see why you wouldn’t.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the intended use of the optional else clause of the try statement?
Is it possible to have a SELECT statement in mySQL with an IF/ELSE clause
Possible Duplicate: Python try-else Comming from a Java background, I don't quite get what
Which was the first version of python to include the else clause for for
How can I add ELSE clause to the following code? ROW_NUMBER() OVER (ORDER BY
SOLVED: This is what was wrong: current.addFolder(folder); (in the final else clause of the
I have an if/else statement in the controller and 2 different types of views
In my application users enter a url and I try to open the link
I'm using a stored procedure in MySQL, with a CASE statement. In the ELSE
Something else perhaps? I am already using nHibernate, but I get occasional issues where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.