CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2002
    Posts
    65

    try{ } catch { }

    I am having technical difficulties on my PC, so compiling and experimenting is not an option. Therefore I need to ask a question on here.

    Question: For the code below. If an exception occurs at line 7 below, then what subsequent lines of code will be executed.
    I want to know if (1) The values for First and Second will ever
    be set to true? And (2)Will a value be returned at line 18. If so,
    will the value be false.

    1 bool SomeClass::SomeMethodOfClass(void)
    2 {
    3 bool First(false);
    4 bool Second(false);
    5
    6 try {
    7 char *ForceError=ABC; // An exception occurs here
    8 int X=5l
    9 Second=true;
    10 }
    11 catch(...)
    12 {
    13 // Handle the exception !
    14 }
    15
    16 First=True; // Will First be set to true if an exception occurs.
    17
    18 return First;
    19
    20 }

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    If an exception is thrown then everything after line #7 would be ignored.

    // Second == false
    // First == true

    Kuphryn

  3. #3
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Actually

    First=True;

    Will be executed, unless the catch rethrows the exception, or returns.

    Jeff

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    Jeff is right. But note that this is only true because you used catch with an ellipsis (...) - which catches all exceptions. If you'd defined 'catch' to trap a particular type of exception, it's possible that 'First = TRUE' might never be reached. That would depend upon the actual exception that occurred.

  5. #5
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208
    If exception is there at line 7
    Second=true; is not going to execute.

    about First = True;
    Its really depends upon what r u doing in catch block.

    Vinod

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured