Try Catch question

Discussion in 'MCAD' started by Ehab Zaky, May 1, 2004.

  1. Ehab Zaky

    Ehab Zaky Guest

    Dear all,
    While studying in some study guide for 316, I found a question that asks for
    the output of the following code.

    try {
    Debug.WriteLine(“Inside Try”);
    throw(new IOException());}
    catch (IOException e) {
    Debug.WriteLine (“IOException Caught”);}
    catch (Exception e) {
    Debug.WriteLine(“Exception Caught”);}
    finally {
    Debug.WriteLine (“Inside Finally”);}
    Debug.WriteLine (“After End Try”);

    - I tried this code and the output as I expected was

    Inside Try
    IOException Caught
    Inside Finally
    After End Try

    - But the answer in the study guide was
    Inside Try
    IOException Caught
    Inside Finally

    Am I right? or what?

    Thanks in advance
    Yours,
    Ehab Zaky
     
    Ehab Zaky, May 1, 2004
    #1
    1. Advertisements

  2. Ehab Zaky

    Yorkie Guest

    You are right!
     
    Yorkie, May 1, 2004
    #2
    1. Advertisements

  3. Ehab Zaky

    Udrea Emil Guest

    You are wrong. Due to the fact that the line code: "throw(new IOException())" exists, all that follows after "..finally block.." won't be done. If you commented this line code, your solution would be correct

    Sincerly
    Emil Udrea
     
    Udrea Emil, May 2, 2004
    #3
  4. Ehab Zaky

    Yorkie Guest

    But wouldn't the Catch clause handle the exception and
    stop it from bubbling up through the stack ?

    Therefore, the processing would continue after
    the 'try..catch..finally' block as the exception has been
    caught and handled, which would mean the 'Debug.WriteLine
    ("After End Try");' statement would be executed.


    (new IOException())" exists, all that follows
    after "..finally block.." won't be done. If you commented
    this line code, your solution would be correct.
     
    Yorkie, May 3, 2004
    #4
  5. Ehab Zaky

    M. Guest

    Yes, you're right.

    This is the correct answer:

    Inside Try
    IOException Caught
    Inside Finally
    After End Try

    Regards,
    M, MCP (70-316)
     
    M., May 4, 2004
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.