Exception Handling Block does not handle InvalidOperationException though it's defined in the policies.

Topics: Data Access Application Block, Exception Handling Application Block
Oct 21, 2013 at 8:21 PM
Edited Oct 21, 2013 at 8:21 PM
Hello there,

The following is the policy definition for handling exceptions.
Dim policyEntries As List(Of ExceptionPolicyEntry) = New List(Of ExceptionPolicyEntry)() From { _
            {New ExceptionPolicyEntry(GetType(InvalidOperationException), PostHandlingAction.NotifyRethrow, New IExceptionHandler() _
                                      {New ReplaceHandler("An internal error to fix happened.", GetType(InvalidOperationException))})}, _
            {New ExceptionPolicyEntry(GetType(DataException), PostHandlingAction.ThrowNewException, New IExceptionHandler() _
                                      {New SqlExceptionHandler()})}
            }
        'New SemanticLoggingErrorHandler(PortalEventSource.Log.LogException, GetType(TextExceptionFormatter)), 
        policies.Add(New ExceptionPolicyDefinition(SQLExceptionPolicy, policyEntries))

        Dim exceptionManager As New Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager(policies)
I use Data Access Block utilizing stored procedures to retrieve data, and intentionally removed a stored Procedure from database to have the following code throw "InvalidOperationException".

The code is in the Process block as following, however Exception Handling block does not pickup the policy to replace the exception and instead it bubbles up exact exception to the presentation layer.
ExceptionManager.Instance.Process(Function()
           Dim criteria As New Hashtable
         Dim roleId As Guid

         criteria.Add("IsVcnaOnly", CurrentUser.UserType = UserTypes.VCNAUser)

    rolePermissions = _context.RolePermissionRepository.GetRolePermissions(criteria).ToList()
                
         Return Json(rolePermissions, JsonRequestBehavior.AllowGet)
   End Function, ExceptionManager.SQLExceptionPolicy)
Thanks in advance for let me know what's missing here.
Mohammad
Oct 22, 2013 at 12:23 AM
If you want to throw the new exception change PostHandlingAction.NotifyRethrow to PostHandlingAction.ThrowNewException. When the PostHandlingAction is set to NotifyRethrow then the Process method will throw the original exception.

So the code that replaces the exception would look something like this:
Dim policies As List(Of ExceptionPolicyDefinition) = New List(Of ExceptionPolicyDefinition)

Dim policyEntries As List(Of ExceptionPolicyEntry) = New List(Of ExceptionPolicyEntry)() From { _
    {
        New ExceptionPolicyEntry(GetType(InvalidOperationException), PostHandlingAction.ThrowNewException, New IExceptionHandler() _
                                {New ReplaceHandler("An internal error to fix happened.", GetType(InvalidOperationException))})}
    }

policies.Add(New ExceptionPolicyDefinition("Default Policy", policyEntries))

Dim exceptionManager As New ExceptionManager(policies)

exceptionManager.Process(Function()
                                Throw New InvalidOperationException()
                            End Function,
                           "Default Policy")

This results in an InvalidOperationException with the message set to "An internal error to fix happened.".

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 10/22/2013 at 11:15 AM
Oct 22, 2013 at 6:14 PM
Thank you Randy. I changed the setup and it worked like a charm.

Cheers,
Mohammad