473,785 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to determine what exceptions a method might raise?

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
5 1296
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
Ed Jensen <ej*****@visi.c omwrites:
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
Ben Finney wrote:
Ed Jensen <ej*****@visi.c omwrites:
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
In <11************ *********@a75g2 000cwd.googlegr oups.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
"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 UnicodeDecodeEr ror, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2186
by: Vegard Bakke | last post by:
From whet I can see, Python documentation is lacking a very important piece of information. Very few functions have documented what exceptions they raise, and under what cicumstances. Is this a task on the schedule? In my opinion, every function that might raise an exception should state so explicitly in the documentation, even uncaught exceptions from a subfunction.
12
2803
by: Raymond Hettinger | last post by:
For your amusement and edification, I'm working on a series of Python puzzles designed to highlight areas of the language known only to those who have read the docs more than once. Each of the following sections contains a code snippet that someone, somewhere might find a little mysterious. Your goal is to sleuth through the code, identify what was expected versus what really happened, and to
26
2649
by: Zunbeltz Izaola | last post by:
Hi, I've the following problem with try/exception. I've a try block that will raise some exceptions. I want the program to ignore this exceptions completely. Is it possible? Thanks in advance Zunbeltz
3
4594
by: Dmitry Prokoptsev | last post by:
Hello, I need to write a class for exceptions which can be thrown, caught, stored and thrown once again. I have written the following code: --- begin --- #include <string> class Exception { public:
5
1210
by: Thomas Lotze | last post by:
Hi, I wonder how to solve the following problem the most pythonic way: Suppose you have a function f which, as part of its protocol, raises some standard exception E under certain, well-defined circumstances. Suppose further that f calls other functions which may also raise E. How to best distinguish whether an exception E raised by f has the meaning defined by the protocol or just comes from details of the implementation?
2
1488
by: sc_wizard29 | last post by:
Hi everyone, I've just finished studying O'Reilly's "Learning python" and since I come from the Java world, there are some things that bother me concerning python's exception handling. In Java, all methods must declare the exceptions throwed (I'm speaking of checked exceptions)... but this is not the case in Python. So my question is : how can I know which exceptions I must catch ? (am I supposed to believe what's written in the...
11
1740
by: Jonathan Wood | last post by:
I must be the only one who doesn't think exceptions are the greatest thing since bread. Consider the following code: id = Convert.ToInt32(Request.QueryString); First, is there an easy way to tell which methods or properties could potentially throw an exception? I've scanned the docs and it doesn't seem to say. Without that knowledge, it's a bit of hit and miss.
28
5895
by: Christoph Zwerschke | last post by:
What is the best way to re-raise any exception with a message supplemented with additional information (e.g. line number in a template)? Let's say for simplicity I just want to add "sorry" to every exception message. My naive solution was this: try: ... except Exception, e: raise e.__class__, str(e) + ", sorry!"
14
4358
by: Rafe | last post by:
Hi, I've encountered a problem which is making debugging less obvious than it should be. The @property decorator doesn't always raise exceptions. It seems like it is bound to the class but ignored when called. I can see the attribute using dir(self.__class__) on an instance, but when called, python enters __getattr__. If I correct the bug, the attribute calls work as expected and do not call __getattr__. I can't seem to make a simple...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.