why my try/catch statement "loops"

hi!

i have three succeeding try-catch-finally statements but it sort of "loops" when i run the program. this is basically the code.. .

try

{

insert1.Connection.Open();

insert1.ExecuteNonQuery();

}

catch (OracleException ex)

{

MessageBox.Show(ex.ToString());

}

finally

{

MessageBox.Show("insert1 done!");

insert1.Connection.Close();

}

try

{

insert2Connection.Open();

insert2.ExecuteNonQuery();

}

catch (OracleException ex)

{

MessageBox.Show(ex.ToString());

}

finally

{

MessageBox.Show("insert2 done!");

insert2.Connection.Close();

}

try

{

insert3.Connection.Open();

insert3.ExecuteNonQuery();

}

catch (OracleException ex)

{

MessageBox.Show(ex.ToString());

}

finally

{

MessageBox.Show("insert 3 done!");

insert3.Connection.Close();

}

So when the program is executed the three message box are shown one after the other but when i click ok on the last message the first message is again shown...then the next and the next.. but this time after clicking ok the statement is finally finished for real.

what is wrong in here someone please tell me my mistake.

thanks!



Answer this question

why my try/catch statement "loops"

  • Dan-Teklynx

    actually i cant find any little green dot on the call stack window...

    but i tried something else.. you see the button (which calls the insert) is coded so this ---

    button1.Click +=new System.EventHandler(button1_Click);

    is coded at the form.cs but when i placed it at the form.designer.cs the "looping" stopped. is there any issue about this


  • Dr.9

    When you are debugging this section of code, place a breakpoint at the first line of code in the method that contains this try/catch section. Then in Visual Studio, select Debug - Windows - Call Stack. Then, whenever the debugger pauses at the break point you should be able to look down the called stack and see which portion of code is calling this method.

    Like the previous poster, I believe the problem is that some other portion of code is calling this section multiple times. If it were me, I would use the Call Stack option to see where the problem is as described above. If this portion of code is indeed being called multiple times then you will hit the breakpoint with the debugger more than once while debugging.

    Good luck and I hope this helps.


  • JUANCARLOSR

    What you are seeing when the code "goes back to the start" is that the method that contains the try/catch statements is being called more than once from another place in your code.  You may not think you are calling that method more than once, but it seems that it is happening based on your description.

    When the debugger hits the break point at the beginning of the method for the second (unwanted time), then select Debug - Windows - Call Stack from the Visual Studio main menu.  This will give you a new window with a list of methods that were called to get you to the breakpoint the debugger currently sits at.  There will be a little green dot next to the current method you are in.  Look down the list and identify the other sections of code that have lead you to the breakpoint you are currently sitting at.  One of those sections of code is the culprit and is calling that method with the breakpoint more than once. 

    It might not be obvious at first but you can find it.  If there is nothing in the calling code that would cause that method to be called twice, look back further, and see if anything else could be looping to cause this bug.  When you double click items in the Call Stack list, the debugger will take you to the different sections of your source code that were called in succession to get you to your current break point.

    Good luck.  If you can't find the answer, then try posting the code that calls the method with the try/catches here and maybe we can figure it out for you.

     


  • IamWasim

    Xeleste wrote:
    when i inserted a breakpoint i found out that after the final try/catch (}) it goes back to the start (private void....) then it repeats the process... i cant find what is wrong here.. this is kind of frustrating.. *sigh*

    Can you please provide the minimum code to reproduce this situation, because this code works fine.



  • RamyaP

    You just execute the method that contains this code twice Because there is nothing wrong with this code that should make it run twice.

    Can you please provide the minimum code to reproduce this situation, because this code works fine.



  • Quimbo

    when i inserted a breakpoint i found out that after the final try/catch (}) it goes back to the start (private void....) then it repeats the process... i cant find what is wrong here.. this is kind of frustrating.. *sigh*
  • pavel989

    there is nothing wrong in the code. if you check where you call this procedure you can shortout the problem. anyway the method is called two times.

  • chamsoft

    If you call this line twice, it may have caused the bug you described:

    button1.Click +=new System.EventHandler(button1_Click);



  • why my try/catch statement "loops"