Eren Ersönmez already mentioned the most important benefit.
I just want to add that when an exception is caught, it has the effect of unwinding the stack. That means that if you rethrow the exception with throw;
and it isn't caught higher up the stack, the debugger will highlight the throw
, not the place where the exception was initially thrown, so you can't see the state of the variables when the exception was thrown.
On the other hand, exception filters do not unwind the stack, so if the exception isn't caught, the debugger will show the place where the exception was initially thrown.
So if you have this:
try
{
DoSomethingThatThrows();
}
catch (Win32Exception exception)
{
if (exception.NativeErrorCode == 0x00042)
{
//Do something here
}
else
{
throw;
}
}
the debugger will stop on throw
when NativeErrorCode
is not 0x42.
But if you have this:
try
{
DoSomethingThatThrows();
}
catch (Win32Exception exception) if (exception.NativeErrorCode == 0x00042)
{
//Do something here
}
the debugger will stop in DoSomethingThatThrows
(or in a method called by it), letting you see more easily what caused the error (again, when NativeErrorCode
is not 0x42)