Wednesday, March 03, 2010
Module RandomCrash

    Sub Main()
        Try
            Throw New Exception
        Catch ex As Exception When DateTime.Now.Ticks Mod 2 = 0

        End Try
    End Sub

End Module
posted on Wednesday, March 03, 2010 1:04:58 PM (Pacific Standard Time, UTC-08:00)  #    Comments [3]

kick it on DotNetKicks.com
Wednesday, March 03, 2010 8:18:09 PM (Pacific Standard Time, UTC-08:00)
Pure awesome!
Scott Allen
Wednesday, March 03, 2010 8:55:46 PM (Pacific Standard Time, UTC-08:00)
even simpler :

if (DateTime.Now.Ticks Mod 2 == 0)
throw new Exception();

Julien
Julien Ferraro
Thursday, March 04, 2010 7:55:56 AM (Pacific Standard Time, UTC-08:00)
@Julian:

That doesn't quite work the same way. In your version, the code sometimes throws, but the VB version sometimes catches. This isn't precisely expressible in C#, because C# doesn't support exception filters. One could catch and then rethrow, but that's not really the same thing. The IL and jitted code are quite different!
Comments are closed.