Custom Exception Handler

Topics: Exception Handling Application Block
Apr 2, 2007 at 9:50 AM
Hi.

I have downloaded v3.0 Enterprise library and now I'm trying to use it in my project. I have been using v2.0 so far. In my project I have a class which implements IExceptionHandler and it works nicely in v2.0 version.

However in configuration utility which came with the new version of Enterprise library i cannot select this class as a custom exception handler. Message says that there is no classes which implements IExceptionHandler in assembly. My custom exceptions are recognized with no problems.

Do I need to change my custom exception handler class so that it can work with 3.0? Any advice?

Thanks in advance.
Apr 2, 2007 at 12:42 PM
Edited Apr 2, 2007 at 12:44 PM
The Application Block Software Factory holds all secrets to extending Enterprise Library with your own providers. I recommend running it to create a custom exception handler ( untyped ) or at least to see what it expects for configuration tool support.

In general, an untyped custom exception handler looks like the following:

[ConfigurationElementType(typeof(CustomHandlerData))]
public class MyCustomExceptionHandler : IExceptionHandler
{
    /// <summary>
    /// <para>Initializes a new instance of the <see cref="MyCustomExceptionHandler"/>.</para>
    /// </summary>
    /// <param name="values">The value collection used to set the runtime values.</param>
    public MyCustomExceptionHandler(NameValueCollection values)
    {
        // TODO: Use the NameValueCollection object to set runtime values for the MyCustomExceptionHandler
    }
 
 
    #region IExceptionHandler Members
 
    /// <summary>
    /// Handles the exception.
    /// </summary>
    /// <param name="exception">The original exception.</param>        
    /// <param name="handlingInstanceId">The unique identifier attached to the handling chain for this handling instance.</param>
    /// <returns>Modified exception to pass to the next exceptionHandlerData in the chain.</returns>
    public Exception HandleException(Exception exception, Guid handlingInstanceId)
    {
        throw new Exception("The method or operation is not implemented.");
    }
 
    #endregion
 
}

My guess is that you are missing the attribute:

[ConfigurationElementType(typeof(CustomHandlerData))]

which is why the configuration tool cannot find your custom exception handler. You will need to implement the constructor, too.

The error is misleading which makes it a bit confusing.

Regards,

Dave

________________________

David Hayden
Microsoft MVP C#
May 30, 2008 at 8:54 PM
I added the attribute but still it is throwing the same error. Below is my code. Kindly let me know what i'm missing or doing wrong ?

[ConfigurationElementType(typeof(CustomHandlerData))]
public class EB_CustomHander : IExceptionHandler
{
NameValueCollection  nvc = new NameValueCollection();
public EB_CustomHander(NameValueCollection values)
{
    nvc.Add(values);
}
#region IExceptionHandler Members
public Exception HandleException(Exception exception, Guid handlingInstanceId)
{
Logger.Write("Msg from my custom handler");
return exception;
}
#endregion
}
May 30, 2008 at 9:14 PM

Can you post the exception details?

Fernando


APeter wrote:
I added the attribute but still it is throwing the same error. Below is my code. Kindly let me know what i'm missing or doing wrong ?

[ConfigurationElementType(typeof(CustomHandlerData))]
public class EB_CustomHander : IExceptionHandler
{
NameValueCollection  nvc = new NameValueCollection();
public EB_CustomHander(NameValueCollection values)
{
    nvc.Add(values);
}
#region IExceptionHandler Members
public Exception HandleException(Exception exception, Guid handlingInstanceId)
{
Logger.Write("Msg from my custom handler");
return exception;
}
#endregion
}


May 31, 2008 at 1:15 AM
It got resolved. I closed and opened the "Configurator Tool", then i was able to select Custom Handler Type.

Thanks.