Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to determine what exceptions a method might raise?

Reply
Thread Tools

How to determine what exceptions a method might raise?

 
 
Ed Jensen
Guest
Posts: n/a
 
      01-16-2007
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.
 
Reply With Quote
 
 
 
 
Harlin Seritt
Guest
Posts: n/a
 
      01-16-2007
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.


 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      01-16-2007
Ed Jensen <(E-Mail Removed)> 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

 
Reply With Quote
 
George Sakkis
Guest
Posts: n/a
 
      01-17-2007
Ben Finney wrote:

> Ed Jensen <(E-Mail Removed)> 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

 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      01-17-2007
In <(E-Mail Removed). 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
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      01-17-2007
"George Sakkis" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
nuby: determine method passed and determine the receiver that received the method Peņa, Botp Ruby 1 01-24-2004 07:51 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments