-1
\$\begingroup\$

We know when we call HttpContext.Current.Response.Redirect("http://tvrowdy.in"); It throw an exception. Can we do it this way(working fine). Need suggestions and improvements.

public static void ReferToPage(string strPageName)
{
    try
    {
        HttpContext.Current.Response.Redirect(strPageName, false);
    }
    catch
    {

    }
}
\$\endgroup\$

closed as not a real question by svick, Johntron, Glenn Rogers, Jeff Vanzella, James Khoury Jun 11 '13 at 2:27

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ. codereview.stackexchange.com/faq#close \$\endgroup\$ – Johntron Jun 7 '13 at 3:09
  • 1
    \$\begingroup\$ The code that you show is pointless, because sending false as the second parameter keeps the Redirect method from throwing an exception. There will never be any exception to catch (unless you call it too late so that the redirection is not possible any more). \$\endgroup\$ – Guffa Jun 7 '13 at 16:03
2
\$\begingroup\$

I'd recommend against using 'catch-all' blocks unless absolutely necessary.

Response.Redirect always throws a ThreadAbortedException by design (like it or not), unless you pass in the false for the second parameter.

If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes. This exception has a detrimental effect on Web application performance, which is why passing false for the endResponse parameter is recommended.

Any other exceptions it throws indicate a invalid parameter or bad program state. You probably don't want to just ignore these errors and move on as though nothing has happened. I recommend you just do this:

public static void ReferToPage(string strPageName)
{
    HttpContext.Current.Response.Redirect(strPageName, false);
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Would removing try catch block impact on performance? \$\endgroup\$ – vikas Jun 7 '13 at 8:17
  • \$\begingroup\$ @vikas It won't impact performance at all. Catch-all blocks are just bad practice. \$\endgroup\$ – p.s.w.g Jun 7 '13 at 13:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.