Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle exceptions during an ASP.NET request lifecycle
    primarykey
    data
    text
    <p>This question is kind of related to <a href="/questions/594085/handle-uri-hacking-gracefully-in-asp-net">Handle URI hacking gracefully in ASP.NET</a> in that it's too about how to best handle exceptions that occur during an ASP.NET request lifecycle. I've found a way to handle most exceptions gracefully, but then I've found that some exceptions occur so late in the request that there's no way to do stuff like <code>Server.Transfer</code> to compartementalize the whole error presentation logic into its own page.</p> <p>So, I have to handle the exception inside the <code>Application_Error</code> event instead, and do <code>Response.Write</code>s and whatnot. It's ugly. I understand that in some circumstances the response stream could have already been flushed, so transferring the request isn't really an option. What I want to ask is if there's anyone who's found an elegant solution to this problem?</p> <p>Also, I find it difficult to know when I can handle the exception gracefully by transferring the request to another page and not. What is the best way to find out where in the request lifecycle we're at when an exception occurs? If it occurs during the loading and rendering of a page, <code>Page_Error</code> will be able to handle it and I've not had a problem doing <code>Server.Transfer</code> there yet. But if the exception occurs either too early or too late for <code>Page_Error</code> to catch it and it bubbles to <code>Application_Error</code>, what do I do to know whether it's early or late?</p> <p>If it's late in the lifecycle, I'll probably have to do <code>Response.Write</code> directly from <code>Application_Error</code>, but if it's early I can do <code>Server.Transfer</code>. The problem is that trying to do <code>Server.Transfer</code> will itself cause an exception if it's too in the request to do it.</p> <p>So, is there a global enumeration or something similar that will indicate whether it's too late to do creative stuff with the response or not?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload