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

questions about Exceptions?

P: n/a
is there a general philosophy as to when to use exceptions and when
not to?

like here:
def Calc():
global nbr
print eval(nbr)
print "Not computable"
nbr = ""

i have a calculator and nbr is a string that contains '0123456789+-*/'

if the string ends on +-*/ it will throw an exception(unexpected EOF).

i could easily prevent the exceptions here with an if-statement, is
that preferrable and why?
also when u throw exceptions should u catch the speicfic one? i guess
only if u want it to do soemthing special since catching only only
one exception logicall would abort the program if another one is
Apr 10 '08 #1
Share this Question
Share on Google+
2 Replies

P: n/a
In general you should only catch the exceptions you want to catch,
therefore avoiding the issue of catching "unexpected" ones, for
instances the programming unexpectandly closing.

Well, exception handling is expensive (when it catches one) so it
really is up to you. If you are using eval and know it might "EOF"
then you should probably look to handle that. The main IF statement
style I can think of (checking the end of the string) wouldn't be much
of an improvement.

Currently I would be very worried about seeing that code as it breaks
a number of "conventions". However it depends on the importance of the
code to wherever or not you should change this. (Global variable, the
use of Eval, the CATCH ALL except and the setting of a global variable
at the end.)

I've seen a good few (simple and advanced) calculator examples using
python on the NET, it might be worth looking at some to see their
style of coding a calculator to help your own.
Apr 10 '08 #2

P: n/a
the erase() id alwys need if u wanna abort whilst u wrote something.

But if it is meant to only evaluate once you've pressed the enter key
(I take it?) you shouldn't need that. And if you are to abort while
evaluating it will not do that.
Apr 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.