questions about Exceptions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skanemupp@yahoo.se

    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(unexp ected 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?
  • cokofreedom@gmail.com

    #2
    Re: questions about Exceptions?

    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 "convention s". 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.

    Comment

    • cokofreedom@gmail.com

      #3
      Re: questions about Exceptions?

      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.

      Comment

      Working...