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

  • SEARCH
  • Home
  • 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 6387121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:06:44+00:00 2026-05-25T03:06:44+00:00

All right, I’ve seen this multiple times in the past, but most recently with

  • 0

All right,

I’ve seen this multiple times in the past, but most recently with my question here. So, I’m curious why this is the case, in python because generators use exceptions to indicate the end of the data.

If this is so bad for everyone using python, why does the language include it in what are considered fundamental control structures? For those who want to read the relevant PEP go here.

  • 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-25T03:06:45+00:00Added an answer on May 25, 2026 at 3:06 am

    Because ending the generator is not a common event (I know it will always happen, but it only happens once). Throwing the exception is considered expensive. If an event is going to succeed 99% of the time and fail 1%, using try/except can be much faster than checking if it’s okay to access that data (it’s easier to ask forgiveness than permission).

    There’s also a bias against it since try/except blocks used like that can be very difficult to understand. The flow control can be difficult to follow, while an if/else are more straightforward. The try/except means you have to track the flow control of the statements inside the try and inside of the functions it calls (as they may throw the exception and it may propagate upwards. An if/else can only branch at the point when the statement is evaluated.

    There are times when using try/except is correct and times when if/else make more sense. There are also performance costs associated with each of them. Consider:

    a = <some dictionary>
    if key in a:
        print a[key]
    

    vs.

    a = <some dictionary>
    try:
        print a[key]
    except KeyError:
        pass
    

    The first will be faster if key does not exist inside of a and will only be slightly (almost unnoticeable) slower if it does exist. The second will be faster if the key does exist, but will be much slower if it doesn’t exist. If key almost always exists, you go with the second. Otherwise, the first works better.

    EDIT: Just a little thing to add about Python try/except that helps greatly with one of the readability problems.

    Consider reading from a file.

    f = None
    try:
        f = open(filename, 'r')
        ... do stuff to the file ...
    except (IOError, OSError):
        # I can never remember which one of these Python throws...
        ... handle exception ...
    finally:
        if f:
            f.close()
    

    Now anything in the do stuff to the file can throw an exception and we’ll catch it. Commonly, you try to keep as little code as possible in the try for this reason. Python has an optional else clause for the try that will only be run if the try ran to completion without hitting an exception.

    f = None
    try:
        f = open(filename, 'r')
    except (IOError, OSError):
        pass
    else:
        ... do stuff to the file ...
    finally:
        if f:
            f.close()
    

    In this case, you would not have any of the readability problems since only one statement is in the try; it is a python standard library function call and you’re catching only specific exceptions.

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

Sidebar

Related Questions

All right, I've seen some posts asking almost the same thing but the points
All right this is kind of embarrassing, but I am not sure what is
All right, this must be an absolutely easy question, and I apologize for that.
All right, this question requires a bit of reading on your side. I'll try
Right now all of my times on my reports are in GMT. Anybody know
All right guys and gals it's time for the age old question, how do
All right, you guys were very helpful with my last question, so I'll try
All right. The problem here is pretty abstract. Bear with me. I have a
All right, this one is going to sound very weird and I don't know
All right real quick question guys. I know that C# Forms have the OnPaint()

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.