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

The Archive Base Latest Questions

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

I have a hard time figuring this one out, it’s about mistakes that can

  • 0

I have a hard time figuring this one out, it’s about mistakes that can be done when raising an exception in Python 2.7:

try:
  raise [1, 2, 3, 4]
except Exception as ex:
  print ex

the message here is “exceptions must be old-style classes or derived from BaseException, not list” – This part is ok, but when I change it to tuple, I am getting confused:

try:
  raise (1, 2, 3, 4)
except Exception as ex:
  print ex

the message here is “exceptions must be old-style classes or derived from BaseException, not int” – why is it interpreted as raising an int, not a tuple?

Futhermore:

try:
  raise (Exception, 'a message')
except Exception as ex:
  print ex

Here we are actually rising an Exception (consistent behaviour when compared with previous example, where we were raising an int) – I briefly thought that this is just an alternate way for this:

try:
  raise Exception, 'a message'
except Exception as ex:
  print ex

But in this case, ‘a message’ is being passed to Exceptions ctor (as documented on docs.python.org)

Can someone explain the 2nd and 3rd cases, and possible point me to code in interpreter that is responsible for this?

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

    As documented in the Python 2 reference, the raise statement takes up to 3 expressions to create the exception being raised:

    raise_stmt ::= “raise” [expression [“,” expression [“,” expression]]]

    If the first expression is a tuple, python will ‘unwrap’ the tuple recursively, taking the first element until it finds something other than a tuple. This behavior is being removed from Python 3 (see PEP 3109). The following is legal:

    >>> raise ((Exception, 'ignored'), 'ignored'), 'something', None
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    Exception: something
    

    The documentation explains the rest in more detail, but the raise statement expects the first value to be a Exception class, the second value is seen as the value of the exception (the message) and the third value is a traceback. Python fills in None for the latter two values if missing.

    If the first value is a instance instead, the second value must be None:

    >>> raise Exception('something'), 'something', None
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: instance exception may not have a separate value
    

    If you use a tuple of more than 3 items, it’ll raise a syntax error:

    >>> raise Exception, 'something', None, None
      File "<stdin>", line 1
        raise Exception, 'something', None, None
                                          ^
    SyntaxError: invalid syntax
    

    In your case however, you raised neither a class nor an instance, so that’s what Python found to be incorrect first; if I use a string it’ll complain too:

    >>> raise 'not an exception', 'something', None
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: exceptions must be old-style classes or derived from BaseException, not str
    

    The correct syntax is of course:

    >>> raise Exception, 'something', None
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    Exception: something
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a hard time figuring out the best way to do this... I have
Having a hard time figuring out the best way to go about this. What
I am coming from C# background and I have hard time figuring out how
I am having a hard time on this one, I have a folder over
I've had a hard time figuring out how I can write a struct in
I'm having a hard time figuring this problem out - I am trying to
I'm having a hard time figuring out how to association one of my models
Apologies if this is a repeat; I'm having a hard time figuring out what
I have a hard time figuring out how to set a property with the
I'm pretty new to this stuff and having a hard time figuring out how

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.