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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:46:13+00:00 2026-05-14T02:46:13+00:00

I find I’ve been confused by the problem that when I needn’t to use

  • 0

I find I’ve been confused by the problem that when I needn’t to use try..except.For last few days it was used in almost every function I defined which I think maybe a bad practice.For example:

class mongodb(object):

    def getRecords(self,tname,conditions=''):
        try:
            col = eval("self.db.%s" %tname)
            recs = col.find(condition)
            return recs
        except Exception,e:
            #here make some error log with e.message

What I thought is ,exceptions may be raised everywhere and I have to use try to get them.
And my question is,is it a good practice to use it everywhere when defining functions?If not are there any principles for it?Help would be appreciated!

Regards

  • 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-14T02:46:13+00:00Added an answer on May 14, 2026 at 2:46 am

    That may not be the best thing to do. Whole point of exceptions is that you can catch them on very different level than it’s raised. It’s best to handle them in the place where you have enough information to make something useful with them (that is very application and context dependent).

    For example code below can throw IOError(“[Errno 2] No such file or directory”):

    def read_data(filename):
        return open(filename).read()
    

    In that function you don’t have enough information to do something with it, but in place where you actually using this function, in case of such exception, you may decide to try different filename or display error to the user, or something else:

    try:
        data = read_data('data-file.txt')
    except IOError:
        data = read_data('another-data-file.txt')
        # or
        show_error_message("Data file was not found.")
        # or something else
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find it odd that in Visual C# 2008 Express edition, when you use
I find that getting Unicode support in my cross-platform apps a real pain in
I find that the .NET event model is such that I'll often be raising
I find it hard to believe this hasn't been asked before, but it doesn't
I find it hard to believe that every developer creates their icons manually in
I find the concept of the programmable, tiling, keyboard-focuessed window manager ion3 very appealing,
I find from reading perldoc perlvar, about a thousand lines in is help for
I find I can do more with NHibernate, and even Castle than with the
I find myself writing delegates occasionally for really simple functions (take no arguments and
I find it much more difficult to manage your session in a desktop application,

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.