views:

4175

answers:

6

Hi, I have an ASP.NET web application, and I wanted to know how I could display an error message box when an exception is thrown.

For example,

        try
        {
            do something
        }
        catch 
        {
            messagebox.write("error"); 
            //[This isn't the correct syntax, just what I want to achieve]
        }

[The message box shows the error]

Thank you

A: 

Is this a good idea? You could do it with javascript but even then I'm not sure I would do it.

Bobby Cannon
Let's say I don't want to display the whole exception, how else do I tell the user there is an error, and tell the user error details?
zohair
+8  A: 

You can't reasonably display a message box either on the client's computer or the server. For the client's computer, you'll want to redirect to an error page with an appropriate error message, perhaps including the exception message and stack trace if you want. On the server, you'll probably want to do some logging, either to the event log or to a log file.

 try
 {
     ....
 }
 catch (Exception ex)
 {
     this.Session["exceptionMessage"] = ex.Message;
     Response.Redirect( "ErrorDisplay.aspx" );
     log.Write( ex.Message  + ex.StackTrace );
 }

Note that the "log" above would have to be implemented by you, perhaps using log4net or some other logging utility.

tvanfosson
Upvoted for saying what I was trying to say.
Bobby Cannon
@tvanfosson - It is not that they cannot display message box on server side or client side. They should just avoid doing so. :)
Ramesh
@Ramesh -- clarified.
tvanfosson
+8  A: 

You cannot just call messagebox.write cause you are disconnected from the client. You should register javascript code that shows a messagebox:

this.RegisterClientScriptBlock(typeof(string), "key",  string.Format("alert('{0}');", ex.Message), true);
Michiel Overeem
I prefer a separate error page if the exception would cause problems with the rendering of the requested page.
tvanfosson
I think it depends on the context if an alert or a separate page is the right solution.
Michiel Overeem
A: 

The way I've done this in the past is to populate something on the page with information when an exception is thrown. MessageBox is for windows forms and cannot be used for web forms. I suppose you could put some javascript on the page to do an alert:

Response.Write("<script>alert('Exception: ')</script>");
Perchik
+1  A: 

I wouldn't think that you would want to show the details of the exception. We had to stop doing this because one of our clients didn't want their users seeing everything that was available in the exception detail. Try displaying a javascript window with some information in it explaining that there has been a problem.

Jeremy Cron
+1  A: 

using MessageBox.Show() would cause a message box to show in the server and stop the thread from processing further request unless the box is closed.

What you can do is,

this.Page.ClientScript.RegisterStartupScript(this.GetType(),"ex","alert('" + ex.Message + "');", true);

this would show the exception in client side, provided the exception is not bubbled.

Ramesh