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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:12:27+00:00 2026-05-31T19:12:27+00:00

some_function() raises an exception while executing, so the program jumps to the except :

  • 0

some_function() raises an exception while executing, so the program jumps to the except:

try:
    some_function()
except:
    print("exception happened!")

How do I see what caused the exception to occur?

  • 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-31T19:12:28+00:00Added an answer on May 31, 2026 at 7:12 pm

    The other answers all point out that you should not catch generic exceptions, but no one seems to want to tell you why, which is essential to understanding when you can break the “rule”. Here is an explanation. Basically, it’s so that you don’t hide:

    • the fact that an error occurred
    • the specifics of the error that occurred (error hiding antipattern)

    So as long as you take care to do none of those things, it’s OK to catch the generic exception. For instance, you could provide information about the exception to the user another way, like:

    • Present exceptions as dialogs in a GUI
    • Transfer exceptions from a worker thread or process to the controlling thread or process in a multithreading or multiprocessing application

    So how to catch the generic exception? There are several ways. If you just want the exception object, do it like this:

    try:
        someFunction()
    except Exception as ex:
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print message
    

    Make sure message is brought to the attention of the user in a hard-to-miss way! Printing it, as shown above, may not be enough if the message is buried in lots of other messages. Failing to get the users attention is tantamount to swallowing all exceptions, and if there’s one impression you should have come away with after reading the answers on this page, it’s that this is not a good thing. Ending the except block with a raise statement will remedy the problem by transparently reraising the exception that was caught.

    The difference between the above and using just except: without any argument is twofold:

    • A bare except: doesn’t give you the exception object to inspect
    • The exceptions SystemExit, KeyboardInterrupt and GeneratorExit aren’t caught by the above code, which is generally what you want. See the exception hierarchy.

    If you also want the same stacktrace you get if you do not catch the exception, you can get that like this (still inside the except clause):

    import traceback
    print traceback.format_exc()
    

    If you use the logging module, you can print the exception to the log (along with a message) like this:

    import logging
    log = logging.getLogger()
    log.exception("Message for you, sir!")
    

    If you want to dig deeper and examine the stack, look at variables etc., use the post_mortem function of the pdb module inside the except block:

    import pdb
    pdb.post_mortem()
    

    I’ve found this last method to be invaluable when hunting down bugs.

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

Sidebar

Related Questions

I was looking at some lazy loading property decorators in Python and happened across
I have found different articles about this exception but none of them was my
The function code: # Connect to the DB try: dbi = MySQLdb.connect(host='localhost', \ user='user',
I am performing some network related code. When the code starts executing and the
I have a python function that may raise an exception. The caller catches the
Consider this begin-rescue-ensure block: attempts=0 begin make_service_call() rescue Exception retry unless attempts>2 exit -1
I have some code that throws an exception as follows. if (jobFinished) { ...
try { // some error } catch (any e) { e.extendedInfo = 'New extended
What is the best way to checkt o see if the TelephoneNumber property has
Currently I have some code deleting some temporary files created by my program: #

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.