How do I stop running a function?  
Author Message
Quilnux





PostPosted: Visual C# General, How do I stop running a function? Top

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



Visual C#9  
 
 
RizwanSharp





PostPosted: Visual C# General, How do I stop running a function? Top

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



 
 
Michael Letterle





PostPosted: Visual C# General, How do I stop running a function? Top

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.

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)

}

}

}




 
 
Quilnux





PostPosted: Visual C# General, How do I stop running a function? Top

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


 
 
Quilnux





PostPosted: Visual C# General, How do I stop running a function? Top

Can't return from a void can I I'm also not clear on what you mean by re-raising the exception.

Quilnux


 
 
RizwanSharp





PostPosted: Visual C# General, How do I stop running a function? Top

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 )

Best Regards,

Rizwan



 
 
Michael Letterle





PostPosted: Visual C# General, How do I stop running a function? Top

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



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)

}

}

}





 
 
RizwanSharp





PostPosted: Visual C# General, How do I stop running a function? Top

Can't return from a void can I I'm also not clear on what you mean by re-raising the exception.

Quilnux

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



 
 
Michael Letterle





PostPosted: Visual C# General, How do I stop running a function? Top

You can return from a void, there's just no return value. By re-raising the exception I mean something like the following:

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)

}

}

}




 
 
Quilnux





PostPosted: Visual C# General, How do I stop running a function? Top

Interesting. I'll try that. thanks.

Quilnux


 
 
Quilnux





PostPosted: Visual C# General, How do I stop running a function? Top

If I can just return then that would work as well.

Thanks,

Quilnux


 
 
Quilnux





PostPosted: Visual C# General, How do I stop running a function? Top

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.
 
 
RizwanSharp





PostPosted: Visual C# General, How do I stop running a function? Top

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.

Best Regards,

Rizwan