Here's the best VB.NET solution I could come up with: strong typing and the syntax is all 'inline' which was one of my problems with the filter one. (I'm starting to see how something like this might be useful for C#.)
Public Shared Function CatchOnly(ByVal ex As Exception, ByVal ParamArray types As Type())
Return Array.IndexOf(types, ex.GetType()) <> -1
End Function
Public Shared Sub TestThrow2(ByVal t As Exception)
Try
Console.WriteLine("Throwing: " & t.ToString())
Throw t
Catch ex As Exception When CatchOnly(ex, GetType(FooException), GetType(BarException))
Console.WriteLine("Caught: " & ex.ToString())
Catch ex As Exception
Console.WriteLine("Didn't Catch: " & ex.ToString())
Finally
Console.WriteLine("Finally")
End Try
End Sub
2:08:36 AM
|