raise Exception or raise Exception()

Discussion in 'Python' started by ernest, Nov 14, 2010.

  1. ernest

    ernest Guest

    I have seen both forms and I'm not sure if they're
    both correct, or one is right and the other wrong.
    In practical terms, the two of them seem to have
    the same effect.

    Cheers,
    Ernest
    ernest, Nov 14, 2010
    #1
    1. Advertising

  2. ernest

    Chris Rebert Guest

    On Sun, Nov 14, 2010 at 8:58 AM, ernest <> wrote:
    > I have seen both forms and I'm not sure if they're
    > both correct, or one is right and the other wrong.


    They're both acceptable (although obviously you should always raise a
    more specific error than Exception).
    `raise SomeException` is in fact equivalent to `raise SomeException()`.
    Providing an error message is always advisable though, so really in
    most cases you ought to `raise SomeException("Informative error
    message")`

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Nov 14, 2010
    #2
    1. Advertising

  3. ernest

    Roy Smith Guest

    In article <>,
    Chris Rebert <> wrote:

    > On Sun, Nov 14, 2010 at 8:58 AM, ernest <> wrote:
    > > I have seen both forms and I'm not sure if they're
    > > both correct, or one is right and the other wrong.

    >
    > They're both acceptable (although obviously you should always raise a
    > more specific error than Exception).
    > `raise SomeException` is in fact equivalent to `raise SomeException()`.


    I've always found this rather disconcerting and unpythonic. Sure, it
    saves a couple of keystrokes here and there, but it also adds confusion
    such as Ernest's. It reminds me of Perl's letting you omit the
    parentheses on function calls.
    Roy Smith, Nov 14, 2010
    #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. GS
    Replies:
    2
    Views:
    5,737
  2. Bengt Richter
    Replies:
    0
    Views:
    344
    Bengt Richter
    Jul 7, 2005
  3. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    394
    Gabriel Genellina
    Feb 5, 2007
  4. Jack Bates
    Replies:
    0
    Views:
    263
    Jack Bates
    May 2, 2011
  5. bvdp

    Raise X or Raise X()?

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

Share This Page