catch (InvalidCastException e) when (e.Data != null) { // Take some action. }