Hello,
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.
Quilnux

How do I stop running a function?
Syed Faraz Mahmood
If I can just return then that would work as well.
Thanks,
Quilnux
vaioks
private void TestFunc(string a, string b)
{
while(a != b)
{
try
{
// Code that should run like normal.
}
catch(Exception ex)
{
OtherFunction(); // call some other function here so control will be moved to that function when Exception is raised here.
}
}
}
I hope this is what you need.
Best Regards,
Rizwan
K Sumeet
private void TestFunc(string a, string b)
{
while(a != b)
{
try
{
// Code that should run like normal.
}
catch(Exception ex)
{
return; // If the code gets here I should stop the entire function right away! (TestFunc)
}
}
}
JezPop
Can't return from a void can I I'm also not clear on what you mean by re-raising the exception.
Quilnux
FassaBortolo
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 )
Best Regards,
Rizwan
mableu
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.
Best Regards,
Rizwan
Hernan93
You can return for a function using return; statement even if return type is void... because return statement will not actually return any object but only return the control to its caller..
Best Regards,
Rizwan
Adrian West
Interesting. I'll try that. thanks.
Quilnux
RJBriscoe
Hello,
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 CallTestFunc()
{
try
{
TestFunc("a", "b");
}
catch(Exception ex)
{
SomeOtherFunc();
}
}
private void TestFunc(string a, string b)
{
while(a != b)
{
try
{
// Code that should run like normal.
}
catch(Exception ex)
{
//cleanup what I need to for this function.
throw(ex);
// If the code gets here I should stop the entire function right away! (TestFunc)
}
}
}
Kebians
drewdb
Hello,
thanks for the response.
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
Dan Rooney
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)
}
}
}