I need to know how I can stop running a function in the middle of running it. I cannot give any real coding examples but, I can get a structural example and show what I mean.

private void TestFunc(string a, string b)

{

while(a != b)

{

try

{

// Code that should run like normal.

}

catch(Exception ex)

{

// If the code gets here I should stop the entire function right away! (TestFunc)

}

}

}

If the code gets to the catch I have to stop executing the function right away and run another function (which is handled elsewhere). This is a very newbie question but, I've not had to do this yet. Thanks for any help.

Initially I thought that this would work too and tried it. however, after, in your example, OtherFunction(); completes it goes right back to this function and continues where it left off at from the while statement. I was also thinking I could just change both variables to equal each other in the while statement but, forgot that there is no way for me to do this (One of the variables is actually handed off from another application. hard to explain why I can't because of the complexity but, just, on the surface I can't). I either need a way to stop the function or, break away from the while statement from within the catch statement.

Well you could re-raise the exception or another exception and have the calling function check for it. Or you can just put a "return" in there.

Re rasing an exception is expensive, secondly returning the function will only return to the calling function and there will be no proof if exception was raised or not...but calling an other function when exception was thorwn will let you know that exception was thrown that's why control has been tranfsered to this function (His question is how to redirect to another piece of code )

Initially I thought that this would work too and tried it. however, after, in your example, OtherFunction(); completes it goes right back to this function and continues where it left off at from the while statement. I was also thinking I could just change both variables to equal each other in the while statement but, forgot that there is no way for me to do this (One of the variables is actually handed off from another application. hard to explain why I can't because of the complexity but, just, on the surface I can't). I either need a way to stop the function or, break away from the while statement from within the catch statement.

Quilnux

If you want the TestFunc to handle what to do after the exception, you answered your own question there as well :) :

private void TestFunc(string a, string b)

{

while(a != b)

{

try

{

// Code that should run like normal.

}

catch(Exception ex)

{

break; // If the code gets here I should stop the entire function right away! (TestFunc)

Another function takes care of exception handling later on, the program has to divert to a whole different segment of code if this 1 function fails. I don't really need this function to report if the expection happened or, not (another function will "just know" later on) but, I do have to prevent further execution of the function or step out of the while (as that is on last statement holding the function running) if the catch is reached. I have been suggested both break and return. I will try each to see which will work best in my application.

Another function takes care of exception handling later on, the program has to divert to a whole different segment of code if this 1 function fails. I don't really need this function to report if the expection happened or, not (another function will "just know" later on) but, I do have to prevent further execution of the function or step out of the while (as that is on last statement holding the function running) if the catch is reached. I have been suggested both break and return. I will try each to see which will work best in my application.

This is a god technique rather than re rasing an exeption gaina after it was raised and it is expensive to re raise exceptions after they are raised and it affects the application's performance.