DCSIMG

 Subscribe in a reader

Creating a custom exception handler - Guy kolbis
Tuesday, August 07, 2007 2:04 PM kolbis

Creating a custom exception handler

With enterprise library 3.1 you can create a custom exception handler that will handle the exception on hand.

First you need to create a custom exception handler. In order to implement your custom handler you must follow 3 rules:

  1. Implement the IExceptionHandler.
  2. Add a class attribute that implements the ExceptionHandlerData (e.g. CustomExceptionData).
  3. Add a constructor that receives a NameValueCollection.

Here is a sample ExceptionHandler that implements those 3 rules:

    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class MyCustomHandler : IExceptionHandler
    {
        public MyCustomHandler(NameValueCollection collection)
        {
        }

        #region IExceptionHandler Members

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            return new ApplicationException(exception.Message, exception);
        }

        #endregion
    }

After you created your implementation of a custom exception handler, you need to define the Custom handler in the Enterprise Library configuration tool.

image

Select your assembly that contains the custom handler class.

image

That is it, you are set to use it. 

תגים:

תוכן התגובה

# re: Creating a custom exception handler

Guy Burstein כתב/ה

You could have done it in Entlib 2.0 as well.

In Enterprise Library 3.1, you can create providers with the application block software factory more easily.

Take a look at how I created a custom validator:

http://www.codeproject.com/useritems/VABCustomValidator.asp

Wednesday, August 08, 2007 12:07 AM

# re: Creating a custom exception handler

kolbis כתב/ה

Hi Guy and thanks for the response and the link. The thing is that in Ent Lib 3.1 you must have a constructor that excepts NameValueCollection, otherwise you will get a compilation error that says "Could not find constructor on type...". In this post i intended to give an overview on how to create it step by step in ent lib 3.1.

Wednesday, August 08, 2007 8:42 AM

# re: Creating a custom exception handler

enrique.prados@a-e.es כתב/ה

Hi misters,

I have an application web (asp.net) and I want define custom Handler for exceptionhandling (Enterprise Library)

My custom class..

   [ConfigurationElementType(typeof(CustomHandlerData))]

   public class GestorExcepcionesIU : IExceptionHandler

I want to do like this:

Exception IExceptionHandler.HandleException(Exception exception, Guid handlingInstanceId)

       {

                 RegisterStartupScript(@@@PAGE@@@, "jsExcepcionIU", "alert('Error IU: " + exception.Message.Replace("'", " ") + "');");

           return exception;

       }

How I get reference of Page in the custom handler ?

Any help will be appreciated, and I'll be very grateful. Thanks in advance.

Greetings, regards.

Please, send me email to: enrique.prados@a-e.es

Thanks a lot

Tuesday, November 13, 2007 11:21 AM

# re: Creating a custom exception handler

zxevil135 כתב/ה

Gtn43k r u crazzy? I told u! I can't read!

Friday, February 29, 2008 5:32 PM

# re: Creating a custom exception handler

Andi כתב/ה

Hi,

In Addition, you have to sign your assembly with a strong name key file! When you don't do this you can't choose your assembly in the Enterprise Lib Configuration Tool, because you get the following Exception-Message:

"There were no types found in the assembly "AssemblyName" that implement or inherit from the base type "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.IExceptionHandler"

Greetings, Andi

Thursday, March 06, 2008 12:57 PM

# re: Creating a custom exception handler

willy88 כתב/ה

Hi, how can I do these thing with Enterprise Library 1.0 (June 2005 Release)? Please help me...

Thanks a lot.

Wednesday, June 10, 2009 9:23 AM

שלח תגובה

(שדה חובה) 
(שדה חובה) 
(אופציונלי)
(שדה חובה) 

Enter the numbers above: