How to determine what exceptions a method might raise?

Discussion in 'Python' started by Ed Jensen, Jan 16, 2007.

  1. Ed Jensen

    Ed Jensen Guest

    I'm really enjoying using the Python interactive interpreter to learn
    more about the language. It's fantastic you can get method help right
    in there as well. It saves a lot of time.

    With that in mind, is there an easy way in the interactive interpreter
    to determine which exceptions a method might raise? For example, it
    would be handy if there was something I could do in the interactive
    interpreter to make it tell me what exceptions the file method might
    raise (such as IOError).

    Thanks in advance.
     
    Ed Jensen, Jan 16, 2007
    #1
    1. Advertising

  2. Hi Ed,

    Generally checking the sources give a very good clue as to what
    exceptions the interpreter can raise. Look around _builtins_ for this.

    Harlin Seritt


    Ed Jensen wrote:
    > I'm really enjoying using the Python interactive interpreter to learn
    > more about the language. It's fantastic you can get method help right
    > in there as well. It saves a lot of time.
    >
    > With that in mind, is there an easy way in the interactive interpreter
    > to determine which exceptions a method might raise? For example, it
    > would be handy if there was something I could do in the interactive
    > interpreter to make it tell me what exceptions the file method might
    > raise (such as IOError).
    >
    > Thanks in advance.
     
    Harlin Seritt, Jan 16, 2007
    #2
    1. Advertising

  3. Ed Jensen

    Ben Finney Guest

    Ed Jensen <> writes:

    > it would be handy if there was something I could do in the
    > interactive interpreter to make it tell me what exceptions the file
    > method might raise (such as IOError).


    For what purpose would this be handy? Surely the benefit of the
    interactive interpreter is that you can simply try it out and *see*
    what happens.

    But, in case it helps: Any code may raise any exception at any
    time. This is a feature, since it encourages program that are tested
    properly.

    --
    \ "I'm a great lover, I'll bet." -- Emo Philips |
    `\ |
    _o__) |
    Ben Finney
     
    Ben Finney, Jan 16, 2007
    #3
  4. Ben Finney wrote:

    > Ed Jensen <> writes:
    >
    > > it would be handy if there was something I could do in the
    > > interactive interpreter to make it tell me what exceptions the file
    > > method might raise (such as IOError).

    >
    > For what purpose would this be handy? Surely the benefit of the
    > interactive interpreter is that you can simply try it out and *see*
    > what happens.
    >
    > But, in case it helps: Any code may raise any exception at any
    > time. This is a feature, since it encourages program that are tested
    > properly.


    That's a silly argument, really, unless perhaps you'd consider a box of
    pills that look like M&Ms a 'feature' since it encourages parents to
    hide them from their kids. A better answer would be along the lines of
    "yes, that would be nice to have but in general it's not possible in a
    dynamic language; that's the price you have to pay for leaving the
    static typing world".

    As for the OP's question, since file is a fixed builtin, I think it
    should be possible to know all the possible exceptions that can be
    raised; I'm not sure if it's clearly documented though.

    George
     
    George Sakkis, Jan 17, 2007
    #4
  5. In <>, George Sakkis
    wrote:
    > Ben Finney wrote:
    >
    >> But, in case it helps: Any code may raise any exception at any
    >> time. This is a feature, since it encourages program that are tested
    >> properly.

    >
    > That's a silly argument, really, unless perhaps you'd consider a box of
    > pills that look like M&Ms a 'feature' since it encourages parents to
    > hide them from their kids. A better answer would be along the lines of
    > "yes, that would be nice to have but in general it's not possible in a
    > dynamic language; that's the price you have to pay for leaving the
    > static typing world".


    I don't thing that's a dynamic vs. static thing because in statically
    typed languages it's not that easy to find out all possible exceptions
    unless the language forces you to declare them.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jan 17, 2007
    #5
  6. Ed Jensen

    Duncan Booth Guest

    "George Sakkis" <> wrote:

    > As for the OP's question, since file is a fixed builtin, I think it
    > should be possible to know all the possible exceptions that can be
    > raised; I'm not sure if it's clearly documented though.


    Just calling 'file' the most obvious exceptions it can raise are:

    IOError
    MemoryError
    ValueError

    I'm not sure if it can return UnicodeDecodeError, it looks as though it may
    be possible on windows, but I couldn't trigger it.

    Assigning the result to a variable can of course raise absolutely any
    exception. That's the main problem: just because a C function raises only a
    few different exceptions doesn't mean that the line of code calling it
    cannot generate a host more.
     
    Duncan Booth, Jan 17, 2007
    #6
    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. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    434
    Gabriel Genellina
    Feb 5, 2007
  2. ernest
    Replies:
    2
    Views:
    308
    Roy Smith
    Nov 14, 2010
  3. Jack Bates
    Replies:
    0
    Views:
    295
    Jack Bates
    May 2, 2011
  4. Peña, Botp
    Replies:
    1
    Views:
    262
    Robert Klemme
    Jan 24, 2004
  5. bvdp

    Raise X or Raise X()?

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

Share This Page