Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Usage Warnings
 Do not raise exceptions in filter b...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Do not raise exceptions in filter blocks

TypeName

DoNotRaiseExceptionsInFilterBlocks

CheckId

CA2219

Category

Microsoft.Usage

Breaking Change

NonBreaking

A method contains a filtered exception handler, and the filter raises exceptions.

When an exception filter raises an exception, the exception is caught by the common language runtime, and the filter returns false. This behavior is indistinguishable from the filter executing and returning false intentionally.

Currently, Visual Basic .NET and the Visual C++ support exception filters.

To fix a violation of this rule, do not raise exceptions in an exception filter.

Do not exclude a warning from this rule. There are no scenarios under which an exception raised by an exception filter provides a benefit to the executing code.

The following example shows a type that violates this rule. Note that the filter appears to return false when it throws an exception.

Visual Basic
Imports System
Module ExceptionFilter
Public Class FilterTest
   
   ' The following function is used as an exception filter.
   ' Violates rule: DoNotRaiseExceptionsInFilterBlocks.
   Public Function AlwaysTrueFilter(label as String) as Boolean
       Console.WriteLine("In filter for {0}.", label)
       ' The following exception terminates the filter.
       ' The common language runtime does not return the exception to the user.
       Throw New ApplicationException("Filter generated exception.")
       Return True
   End Function
   
   Public Sub ThrowException()
   Try
      Throw New ApplicationException("First exception.")

      ' Because the filter throws an exception, this catch clause fails to catch the exception.
      Catch e as ApplicationException When Not AlwaysTrueFilter("First")
            Console.WriteLine("Catching first filtered ApplicationException {0}", e)
            
      ' Because the previous catch fails, this catch handles the exception.
      Catch e as ApplicationException 
      Console.WriteLine("Catching any ApplicationException - {0}", e.Message)
      
    End Try 
    
    ' The behavior is the same when the filter test is reversed.
    Try   
      Throw New ApplicationException("Second exception.")
 
      ' Change the filter test from When Not to When.
      
      ' This catch fails.
      Catch e as ApplicationException When AlwaysTrueFilter("Second")
            Console.WriteLine("Catching second filtered ApplicationException {0}", e)
            
      ' This catch handles the exception.
      Catch e as ApplicationException 
      Console.WriteLine("Catching any ApplicationException - {0}", e.Message)
   End Try  
   End Sub 
   
End Class 
Sub  Main()
        Dim  test as FilterTest = New FilterTest()
        
        test.ThrowException()
   End Sub
End Module

The example produces the following output.

Output

In filter for First.
Catching any ApplicationException - First exception.
In filter for Second.
Catching any ApplicationException - Second exception.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
False Positives      Jonathan Allen   |   Edit   |  

This check can result in false positives. For example, there appears to be no way to write this code without throwing raising this warning.

 Try
m_Listener.Start()
Catch ex As HttpListenerException When ex.Message IsNot Nothing AndAlso ex.Message = "The process cannot access the file because it is being used by another process"
Throw New AsyncListenerException("Could not start Async Listener on " & prefix & "." & vbCrLf & "Check to see if IIS is running.", ex)
End Try

Tags What's this?: Add a tag
Flag as ContentBug
When to Exclude Warnings      Jonathan Allen   |   Edit   |  
This warning can be safely excluded when an exception in the filter expression should be treated as if the filter returned False. Though for performance reasons, it is better to not throw an exception at all.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker