Skip to content

Narrow broad catch (Exception) in AndroidBuildToolsVersion.TryParse#11661

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-narrow-broad-catch
Draft

Narrow broad catch (Exception) in AndroidBuildToolsVersion.TryParse#11661
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-narrow-broad-catch

Conversation

Copilot AI commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

AndroidBuildToolsVersion.TryParse wrapped its parse logic in an empty, undocumented catch (Exception), silently swallowing every exception type — including genuinely unexpected ones — making real bugs indistinguishable from an expected "bad version string" result.

Changes

  • src/Xamarin.AndroidTools/AndroidBuildToolsVersion.cs: Replace the bare catch (Exception) with the three specific types the parse path can actually produce, each documented:
try {
	result = ParseInternal (input);
	return true;
} catch (FormatException) {
	// Non-numeric version component
} catch (OverflowException) {
	// Version component outside the range of Int32
} catch (ArgumentException) {
	// Negative or otherwise invalid version component (Version constructor)
}

Why these three are exhaustive

ParseInternalMonoDroidSdk.ParseVersion only calls int.Parse (FormatException/OverflowException) and new Version (...) (ArgumentOutOfRangeException, a subclass of ArgumentException). Catching the ArgumentException base rather than the narrower derived type is intentional defensiveness.

return false semantics are preserved exactly for malformed strings ("abc", "1.x", "-1.0", "99999999999999999999"); unexpected exceptions now propagate instead of being hidden. No method signatures changed; no other files touched.

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix empty catch block in AndroidBuildToolsVersion.TryParse Narrow broad catch (Exception) in AndroidBuildToolsVersion.TryParse Jun 16, 2026
Copilot AI requested a review from jonathanpeppers June 16, 2026 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants