Take for example the following code:
try
{
Response.Redirect(someurl);
}
finally
{
// Will this code run?
}
Will the code in the finally block run?
Redirect(), code following it will not execute. If you think about it, it would make sense not to execute it. You're basically telling your code that you want to go somewhere else. Example: Think of it as ordering a value meal at McDonalds.
Response. Redirect("Default. aspx", true) means current page execution is terminated and page is redirected to the default.
A finally block always executes, regardless of whether an exception is thrown.
A finally block is always get executed whether the exception has occurred or not. If an exception occurs like closing a file or DB connection, then the finally block is used to clean up the code.
Yes.
Try it and see!
Simple enough to test:
try
{
Response.Redirect(someurl);
}
finally
{
File.WriteAllText("C:\\Temp\\test.txt", "The finally block ran.");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With