raise Exception Syntax question

Discussion in 'Python' started by chris, Apr 9, 2004.

  1. chris

    chris Guest

    hi,
    i am wondering if there is a difference between the following
    possibilities to raise an exception, the first seems to be in all the
    books about python i have but the second gives the same result at least
    under Python 2.3.3:

    >>> import exceptions
    >>> e = exceptions.Exception



    >>> raise e, "MESSAGE"

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    Exception: MESSAGE


    >>> raise e("MESSAGE")

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    Exception: MESSAGE


    is there a preferable way?

    thanks
    chris
     
    chris, Apr 9, 2004
    #1
    1. Advertising

  2. chris

    Peter Hansen Guest

    chris wrote:

    > >>> raise e, "MESSAGE"

    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in ?
    > Exception: MESSAGE
    >
    >
    > >>> raise e("MESSAGE")

    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in ?
    > Exception: MESSAGE
    >
    > is there a preferable way?


    The latter is preferred by many, but in any case note that the
    former is basically translated to the latter by Python itself
    when it executes that code so you might as well go with it.
    The "except" statement catches an instance, after all, so
    explicitly raising one is probably a Good Thing.

    -Peter
     
    Peter Hansen, Apr 10, 2004
    #2
    1. Advertising

  3. chris

    Greg Ewing Guest

    chris wrote:
    > >>> import exceptions
    > >>> e = exceptions.Exception


    There's actually no need to import exceptions, since
    all the standard exception names are built-in. I
    think the exceptions module is just there for
    historical compatibility.

    > >>> raise e("MESSAGE")


    This is the preferred form. Again, the other one
    only exists for historical reasons.

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
     
    Greg Ewing, Apr 16, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bengt Richter
    Replies:
    0
    Views:
    378
    Bengt Richter
    Jul 7, 2005
  2. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    437
    Gabriel Genellina
    Feb 5, 2007
  3. ernest
    Replies:
    2
    Views:
    312
    Roy Smith
    Nov 14, 2010
  4. Jack Bates
    Replies:
    0
    Views:
    297
    Jack Bates
    May 2, 2011
  5. bvdp

    Raise X or Raise X()?

    bvdp, Mar 11, 2012, in forum: Python
    Replies:
    10
    Views:
    409
    Stefan Behnel
    Mar 12, 2012
Loading...

Share This Page