By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

How to determine what exceptions a method might raise?

P: n/a
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.
Jan 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Jan 16 '07 #2

P: n/a
Ed Jensen <ej*****@visi.comwrites:
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

Jan 16 '07 #3

P: n/a
Ben Finney wrote:
Ed Jensen <ej*****@visi.comwrites:
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

Jan 17 '07 #4

P: n/a
In <11*********************@a75g2000cwd.googlegroups. com>, 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
Jan 17 '07 #5

P: n/a
"George Sakkis" <ge***********@gmail.comwrote:
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.

Jan 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.