Displaying Exceptions in ASP.NET

  • Thread starter Thread starter Mike Hixson
  • Start date Start date
M

Mike Hixson

Hi,

In my application I have a lot of functions that use a try.. catch to to
write execption info to a label at the top of my page. Is there anyway that
I could do this at a page-level, instead of a try..catch in every function.
My goal is to allow users to see the execption (like "you cant delete this
from the database") on the page that caused the exception. I dont want to
redirect the users to another page and show an error.

I though something like this would work, but it turns out that the page
dosent render after an exception.

private void Page_Error(object sender, EventArgs e)
{
ErrorMessage.Visible = true;
ErrorMessage.Text = Server.GetLastError().Message;

Server.ClearError();
}

I even tried calling prerender and render in Page_Error, which got me what i
wanted but subsequest requests failed with an error saying that the
viewstate was invalid . Does any know if this is posible?

-Mike
 
I guess that depends on where your code is and when the exception is thrown.
I've had exceptions thrown in Page_Load, get caught in Page_Error, have
page_error log and display the message, and everything was fine.
 
try this java script to display the error message to the use

Response.Write("<script language=\"javascript\">")
Response.Write("alert(\""+WHATEVERSTRING+"\");")
Response.Write("</"+"script>")

This would give the user an alert box with an "OK" button

----- Mike Hixson wrote: ----

Hi

In my application I have a lot of functions that use a try.. catch to t
write execption info to a label at the top of my page. Is there anyway tha
I could do this at a page-level, instead of a try..catch in every function
My goal is to allow users to see the execption (like "you cant delete thi
from the database") on the page that caused the exception. I dont want t
redirect the users to another page and show an error

I though something like this would work, but it turns out that the pag
dosent render after an exception

private void Page_Error(object sender, EventArgs e

ErrorMessage.Visible = true
ErrorMessage.Text = Server.GetLastError().Message

Server.ClearError()


I even tried calling prerender and render in Page_Error, which got me what
wanted but subsequest requests failed with an error saying that th
viewstate was invalid . Does any know if this is posible

-Mik
 
Back
Top