473,326 Members | 2,192 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,326 software developers and data experts.

questions about Exceptions?

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"
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
thrown?
Apr 10 '08 #1
2 998
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
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 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: 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...
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....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.