2 Replies - 170 Views - Last Post: 17 April 2013 - 01:22 PM Rate Topic: -----

#1 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Exception error question!

Posted 17 April 2013 - 12:15 PM

Hey guys, so Im having a really difficult time with this question, I just dont understand how to do it, were are all the numbers coming from if its in range(2), can someone please help thank you. And the answers are B E D.




2- Assume this pseudo-code where “Statement i”, among other things, prints “i”
for i in range(2):
    Statement 1
    try:
        Statement 2
        Statement 3
    except Error 1:
        Statement 4
    except Error 2:
        Statement 5
    else:
        Statement 6
    finally:
        Statement 7


2-a- What would be the output if Statement 2 raises Error 1 each time it is executed?
a- 1 2 3 4 5 6 7
b- 1 2 4 7 1 2 4 7
c- 1 2 3 4 7 1 2 3 4 7
d- 1 2 3 6 7 1 2 3 6 7
e- 1 2 7

2-b- What would be the output if Statement 2 raises Error 3 each time it is executed?
a- 1 2 3 4 5 6 7 error bubbles up
b- 1 2 4 7 1 2 4 7
c- 1 2 3 4 7 1 2 3 4 7
d- 1 2 3 6 7 1 2 3 6 7
e- 1 2 7 error bubbles up

2-c- What would be the output if Statement 2 doesn’t raise any errors?
a- 1 2 3 4 5 6 7 error bubbles up
b- 1 2 4 7 1 2 4 7
c- 1 2 3 4 7 1 2 3 4 7
d- 1 2 3 6 7 1 2 3 6 7
e- 1 2 7 error bubbles up

Is This A Good Question/Topic? 0
  • +

Replies To: Exception error question!

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: Exception error question!

Posted 17 April 2013 - 12:59 PM

Ok, for part a.... when statement 2 raises error 1 it is going to go into except error 1 statement. So for i = 0, it will execute statement 1, statement 2, jump to error 1 which prints statement 4 and then statement 7 because finally blocks are always executed. So we have 1247 when i = 0 and we repeat the same process when i = 1. That is why you have 12471247

part b... same thing. With i = 0 we go to statement 1, then statement 2 but because there is no exception handling for error 3 (we only handle error 1 and error 2) it is going to jump down to the finally block and print seven. But since our error is not handled yet, it is going to bubble up to the caller. So you have a 1 2 7 and a bubble up.

part c... this part is just so you know what the purpose of the else is when it comes to exception handling. The else clause is going to be executed if nothing in the "try" part raises any errors. So this means you are going to print statement 1, then 2, then 3 and since no errors you are going to go to the else and print 6 and then the finally for statement 7... 12367 for when i = 0 and again for i = 1... 1236712367.

Hope that clarifies things for you a bit. To help go to http://docs.python.o...ial/errors.html and do a quick search on that page for "else" and you should see some text half way down the page that talks about that else statement when it comes to error exception handling.

:)

This post has been edited by Martyr2: 17 April 2013 - 01:01 PM

Was This Post Helpful? 1
  • +
  • -

#3 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Exception error question!

Posted 17 April 2013 - 01:22 PM

Oh I see what all the numbers mean now!! Thank you soo much for you help, really appreciate it. 25marks for such a simple question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1