473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Questions on exceptions

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 documentation ? am I supposed
to read the source code to see which exceptions are throwed ?)

Also, can someone explain me why there is no try...except...finally
statement ? For example, the following code snippet is not valid, but
what would be the correct python way to do it ?

myFile = open('file.txt') # assume file exists
try:
for nextLine in file:
nextLine = nextLine.rstrip('\n');print "line = " + nextLine
except IOError:
print "Error while reading from file"
finally:
myFile.close

Aug 21 '06 #1
2 1476
In <11**********************@i3g2000cwc.googlegroups. com>, sc_wizard29
wrote:
Also, can someone explain me why there is no try...except...finally
statement ?
AFAIK historical reasons. There where some concerns about ambiguity. But
in Python 2.5 this form will become legal syntax.
For example, the following code snippet is not valid, but
what would be the correct python way to do it ?

myFile = open('file.txt') # assume file exists
try:
for nextLine in file:
nextLine = nextLine.rstrip('\n');print "line = " + nextLine
except IOError:
print "Error while reading from file"
finally:
myFile.close
You forgot the parenthesis for the `close()` method.

In Python <=2.4 you have to nest:

try:
try:
pass
except Error:
pass
finally:
pass

Maybe you are interested in the new (Python 2.5) ``with`` statement too:

http://docs.python.org/dev/ref/with.html

And the style guide: http://www.python.org/dev/peps/pep-0008/ (because you
used Java naming conventions)

Ciao,
Marc 'BlackJack' Rintsch
Aug 21 '06 #2
On Mon, 21 Aug 2006 00:40:14 -0700, sc_wizard29 wrote:
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 documentation ? am I supposed
to read the source code to see which exceptions are throwed ?)
How do you know what objects a method will return? Are you supposed to
believe the documentation? Are you supposed to read the source code?

Exceptions are no different. If a method claims to raise ValueError, but
sometimes raises TypeError, I call that a bug, regardless of whether the
line which causes it is "int(x) + str(y)" or "raise TypeError('My
documentation is incomplete')".

Also, can someone explain me why there is no try...except...finally
statement ?
Historical reasons. I believe that Python 2.5 will support it.
For example, the following code snippet is not valid, but
what would be the correct python way to do it ?

myFile = open('file.txt') # assume file exists
try:
for nextLine in file:
nextLine = nextLine.rstrip('\n');print "line = " + nextLine
except IOError:
print "Error while reading from file"
finally:
myFile.close
Nested try...except blocks.
--
Steven D'Aprano

Aug 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Old Wolf | last post by:
1. What is the difference between #include <stdexcept> and #include <exception> ? 2. Is there a list somewhere of what each standard exception is used for? either to throw them, or throw...
3
by: Gonçalo Rodrigues | last post by:
Hi all, I've got two somewhat general questions related to exception handling in C++. My usual programming language is the lovely and flexible Python, but for various reasons, including the need...
3
by: Paul | last post by:
In general how do you indentify a specific error? If 2 different error conditions throw the same type of exception how do you know which error occured? There are no error numbers, so are you forced...
11
by: Lloyd Dupont | last post by:
(not I use 2.0, so new return a "normal" pointer and gcnew return a managed one, my question below regarding new concern plain standart C++ allocator) - if I use the default new operator, are all...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
2
by: pack | last post by:
"Unless you have a very good reason to catch an exception, DON'T. Exceptions are supposed to be exceptional, just like the dictionary meaning: uncommon, unusual. When in doubt, let the calling...
33
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: //...
5
by: Soumen | last post by:
For one of my project, I want to use auto_ptr. But I'm facing few issues. 1. Say I've Controller class. I don't want to allow user to create object of this class unless all arguments are valid....
2
by: skanemupp | last post by:
is there a general philosophy as to when to use exceptions and when not to? like here: def Calc(): global nbr try: print eval(nbr) except: print "Not computable"
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.