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 9156117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:42:51+00:00 2026-06-17T12:42:51+00:00

Possible Duplicate: Python FAQ: How fast are exceptions? I remember reading that Python implements

  • 0

Possible Duplicate:
Python FAQ: “How fast are exceptions?”

I remember reading that Python implements a “Better to seek forgiveness than to ask permission” philosophy with regards to exceptions. According to the author, this meant Python code should use a lot of try – except clauses, rather than trying to determine ahead of time if you were about to do something that would cause an exception.

I just wrote some try – except clauses on my web app in which an exception will be raised most of the time the code is run. So, in this case, raising and catching an exception will be the norm. Is this bad from an efficiency point of view? I also remember someone telling me that catching a raised exception has a large performance overhead.

Is it unnecessarily inefficient to use try – except clauses in which you expect an exception to be raised and caught almost all of the time?

Here’s the code — its using the Django ORM to check for objects that associate users with various third party social providers.

try:
    fb_social_auth = UserSocialAuth.objects.get(user=self, provider='facebook')
    user_dict['facebook_id'] = fb_social_auth.uid
except ObjectDoesNotExist:
    user_dict['facebook_id'] = None

try:
    fs_social_auth = UserSocialAuth.objects.get(user=self, provider='foursquare')
    user_dict['foursquare_id'] = fs_social_auth.uid
except ObjectDoesNotExist:
    user_dict['foursquare_id'] = None

try:
    tw_social_auth = UserSocialAuth.objects.get(user=self, provider='twitter')
    user_dict['twitter_id'] = tw_social_auth.uid
except ObjectDoesNotExist:
    user_dict['twitter_id'] = None

The first one will rarely take the exception, since right now we are enforcing “Sign In With Facebook” as the primary method for new users to join the site. But, Twitter and Foursquare are optional, in case they want to import friends or followers, and I expect most people will not.

I’m open to better ways to code this logic.

  • 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-06-17T12:42:52+00:00Added an answer on June 17, 2026 at 12:42 pm

    Whenever you code there is a balancing of concerns: performance, readability, correctness, extendability, maintainability, etc.
    Unfortunately, it is often not possible to improve code in each of these directions at the same time. What is fast may not be as readable for instance.

    One of the reasons why try..except is encouraged in Python is because you often can not anticipate all the ways your code may be used, so rather than checking if a specific condition exists, it is more general to just catch any of a certain class of error that might arise. Thus try..except may make your code more reusable.

    However, it is also true that try..except is slow if the except clause is often being reached.

    Is there a way to code that block so that an exception is not being raised and use try..except to catch the less frequent condition?

    Or if not, for the sake of efficiency, you may choose not to use try..except. There are few hard and fast rules in programming. You have to choose your way based on your balance of concerns.

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

Sidebar

Related Questions

Possible Duplicate: Python package structure Hello, I'm looking to import a python file that
Possible Duplicate: python: how to encrypt a file? I'm trying to make application that
Possible Duplicate: Python: find first element in a sequence that matches a predicate Is
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Python passing list as argument I looked for many topics about that,
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Python: What is the best way to check if a list is
Possible Duplicate: Python: Get object by id Typically when you see the string representation
Possible Duplicate: Python: Behaviour of increment and decrement operators >>> a=2 >>> ++a 2

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.