How to determine what exceptions a method might raise?

E

Ed Jensen

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.
 
H

Harlin Seritt

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
 
B

Ben Finney

Ed Jensen said:
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.
 
G

George Sakkis

Ben said:
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
 
M

Marc 'BlackJack' Rintsch

George Sakkis said:
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
 
D

Duncan Booth

George Sakkis said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top