Exception Handling Block in web application?

Feb 14, 2010 at 2:40 AM

Hello, is it possible to use Exception Handling Application block to show javascript message to client whenever a Exception has occured? I am trying to use this article:

http://msdn.microsoft.com/en-us/library/cc309231.aspx

But messagebox are not for web. Is there any way to replace this with javascipt message that we normally use in cs files? Normally we use this function to show instant javascript messages:

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "script", true);

So, is it doable? I would appreciate any reply/ suggestion.

Feb 15, 2010 at 5:28 AM

Hi,

Can you please try implementing a custom handler (see sample below). And configure a exception policy in your config with a posthandlingaction of None.

 

[ConfigurationElementType(typeof(CustomHandlerData))]
    public class JSExceptionHandler : IExceptionHandler
    {

        public JSExceptionHandler(NameValueCollection ignore)
        {}


        #region IExceptionHandler Members

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {

            Page currentPage = HttpContext.Current.Handler as Page;
            ScriptManager.RegisterClientScriptBlock(currentPage, typeof(Page), "alert", "alert('" + exception.Message + "');", true);
            return exception;
        }

        #endregion
    }

 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Feb 16, 2010 at 3:52 AM

Helo, thanks for your reply. Yes, I was trying implementing custom handler, and setting posthandlingaction to none. The problem is in step 5 of the solution, where it states:

5. Add a new custom handler for the specified exception types, setting the handler type to the new handler created in step 1.

I have added a class that inherits IExceptionHandler, but after adding a custom handler, I cannot set the handler "type" to that class. Its because we are using a ASP .net website project, and it does not create any dll/exe after build. Is there any way to do it?

1 more thing, I am trying to add the "JSExceptionHandler" class in the app_code folder of ASP .net website. In our solution we have other projects such as business layer, storage layer, etc... Does it sound logical if I add the JSExceptionHandler to other projects within our solution? I am saying this because those projects create dlls when built. Anyways, I will try this and get back to you.

Thanks.

Feb 16, 2010 at 4:02 AM
Edited Feb 16, 2010 at 4:09 AM

You really need to put it in a class library.  I suggest you put it in a separate project for classes like, say for helpers or utility classes which doesn't really belong to either business layer or data layer.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 17, 2010 at 10:16 AM

Thanks, Valiant Dudan and Sarah Urmeneta. Yes, putting it into a class library worked. However I had to change a little in the code since Scriptmanager was not available in the class, (also I couldnt make it available). So, I had to change the code a little:

public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {

            Page currentPage = HttpContext.Current.Handler as Page;
            currentPage.ClientScript.RegisterStartupScript(currentPage.GetType(), "alertNewDamn", "alert('" + exception.Message + "');", true);

            return exception;
        }

It worked. Thanks again.

Feb 18, 2010 at 8:13 AM

Hi, Valiant Dudan's code worked, too. In order to make the ScripManager available, I just had to add reference System.web.extension (ajax version), and it worked.