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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:24:13+00:00 2026-05-11T19:24:13+00:00

import sys try: raise xxx except str,e: print 1,e except: print 2,sys.exc_type,sys.exc_value In the

  • 0
import sys
try:
    raise "xxx"
except str,e:
    print "1",e
except:
    print "2",sys.exc_type,sys.exc_value

In the above code a string exception is raised which though deprecated but still a 3rd party library I use uses it.
So how can I catch such exception without relying on catch all, which could be bad.

except str,e: doesn’t catch it? why?

system:
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2

  • 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-11T19:24:14+00:00Added an answer on May 11, 2026 at 7:24 pm

    Here is the solution from python mailing list, not very elegant but will work if can’t avoid the need for such hack

    import sys
    try:
         raise "a string exception"
    except:
         e, t, tb = sys.exc_info()
         if not isinstance(e, str):
              raise    
         print "caught", e
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What does this code mean? try: import thread except ImportError: del _sys.modules[__name__]#why raise But
I am writing some code in Python something like this: import sys try: for
Just curious, why does the following code import sys class F(Exception): sys.stderr.write('Inside exception\n') sys.stderr.flush()
I have some python code that fails: import sys print (MathCheats Times-Ed by jtl999)
The only nice way I've found is: import sys import os try: os.kill(int(sys.argv[1]), 0)
TL/DR: import gc, sys print len(gc.get_objects()) # 4073 objects in memory # Attempt to
I've got a piece of code similar to this: import sys def func1(): func2()
Here's the code I'm using: from PIL import Image import ImageFont, ImageDraw import sys
import sys words = { 1 : 'one', 2 : 'two', 3 : 'three',
import sys sys.path.append('/home/myuser/svn-repos/myproject') from myproject.settings import * But, it says module not found when

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.