1 Replies - 179 Views - Last Post: 30 October 2013 - 11:59 AM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Question about Exception Handling Error

Posted 30 October 2013 - 04:42 AM

I made a simple program, and I have realize a funny behaviour that the program is not behaving with the way that I wanted it to behave.

So to begin with the problem is:

        if (reason == 1):
        if (reason == 2):
    except Error:

I have a conditional where it would test which is true. If an error is thrown at reason 1 at main2(), than I would need to go all back to main() and do the procedure all over again. Is there any way that, if the exception error is thrown, I would still able to be at the same function and do the procedures? Thanks in advancement.

Is This A Good Question/Topic? 0
  • +

Replies To: Question about Exception Handling Error

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 7294
  • View blog
  • Posts: 12,149
  • Joined: 19-March 11

Re: Question about Exception Handling Error

Posted 30 October 2013 - 11:59 AM

So to make this concrete, we find that reason==1, and we execute main(). main() throws an Error exception, and you print an ellipsis. Now we repeat, because we've reached the end of the loop, and we test reason again.

Now what you want is to be at the same point in main() after the exception is handled? No, not without some really stupid and bad writing.
If main throws an exception, it's because it's reached a situation that it can't cope with. If it re-enters that same situation, it has to throw the same exception, because that's all it knows how to do. On the other hand, if you change the scenario, it's going to have to re-calculate from the start - you can't drop into the middle of a function.

What you can do would be to divide your function into two. The first would deal with stuff up to the exception point. After that, you can check to see if the exceptional condition has come up, and if it has, deal with it in some fashion. Then you have a repaired state and you can enter part two of your function. Of course, now the situation is no longer exceptional, since you've dealt with it, and you could in principle graft the divided function back together, but it might make more sense to keep it separated. Functions should be small and coherent and unitary, so if the division makes two simpler and more coherent functions, then you should keep those.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1