Skip to content

ref: Narrow Throwable catches to the exceptions they guard (JAVA-552) - #6669

Draft
runningcode wants to merge 5 commits into
9.x.xfrom
no/java-552-narrow-catches
Draft

runningcode wants to merge 5 commits into
9.x.xfrom
no/java-552-narrow-catches

Conversation

@runningcode

@runningcode runningcode commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

📜 Description

Replace 93 catch (Throwable) with the exceptions the guarded code can throw. SDK bugs and fatal errors at these sites are no longer swallowed:

  • Executors (36): RejectedExecutionException around submit and schedule, which ISentryExecutorService declares.
  • File I/O: IOException. SecurityException for File.delete, renameTo and exists, which can throw nothing else.
  • ClientReportRecorder.attachReportToEnvelope: IOException, which SentryEnvelopeItem.fromClientReport declares. The name is fully qualified to keep this PR out of the import block, where fix: Rethrow fatal errors at the SDK's boundary catches (JAVA-552) #6665 adds ExceptionUtils on the adjacent line and the two PRs would conflict.
  • Future.get():
    • DefaultAndroidEventProcessor (3): InterruptedException, which now restores the interrupt flag, and ExecutionException.
    • SendCachedEnvelopeIntegration: its broad arm is replaced by ExecutionException and CancellationException. The executor returns a cancelled future instead of throwing when its queue is full.
  • Parsing and the rest: the specific types, such as NumberFormatException, IllegalArgumentException, MalformedURLException, URISyntaxException and PatternSyntaxException.
    • Baggage's key/value parsing also catches IndexOutOfBoundsException, which an incoming entry without = triggers.
    • NativeEventCollector catches IllegalStateException, which is how the JSON reader reports an unexpected token, and NumberFormatException for an item length that isn't an int.
    • BaseCaptureStrategy's inline segment persistence catches IOException, and IndexOutOfBoundsException for a corrupt segment file line.
  • Where no narrower type exists, with a comment saying why:
    • Exception where the guarded code is declared to throw it: SentryEnvelopeItem.getData() in NativeEventCollector and TombstoneIntegration, and the meta length Callable in SentryEnvelopeItemHeader.
    • RuntimeException where the code throws a plain one: android.os.Debug in AndroidProfiler, and Installation.id.
    • ManifestModulesLoader catches IOException | RuntimeException: the class loader belongs to the app or its container, and can throw an IllegalStateException for a jar that was already closed. The modules found so far are still cached.
    • ContextUtils.getFamily and BuildInfoProvider.isEmulator catch NullPointerException, since Build fields are only null off-device. Their signatures are unchanged.
  • CanvasStrategy.postSafely: Handler.post doesn't throw. It returns false when the looper is quitting, so the catch is replaced by a check of that result.

Mixed sites

These sites need more than one kind of change, so they get their full treatment here:

  • The three Future.get() catches in DefaultAndroidEventProcessor: narrowing, plus restoring the interrupt. (SendCachedEnvelopeIntegration already keeps the interrupt on 9.x.x, so it's only narrowed.)

Test changes

Several tests only passed because these catches hid mocks that don't behave like the real objects. They now do:

  • The Replay executor mocks returned Unit from submit instead of a Future.
  • ContextUtilsTestHelper.createMockContext gave the mocked Context and Application no assets. A real Context always has them.
  • RootCheckerTest's mocked Runtime.exec returned null.
  • EnvelopeCacheTest faked a session write failure with a RuntimeException; it now throws an IOException.
  • The batch processor tests make submit throw RejectedExecutionException instead of a plain RuntimeException.

💡 Motivation and Context

This is part of the catch (Throwable) audit. It replaces #6564, and the narrowings in #6657, #6658 and #6659.

This PR is independent of its siblings and they can merge in any order:

💚 How did you test it?

  • The existing unit tests of the changed modules, with the mock fixes above, plus detekt for the Kotlin modules.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

The changelog entry for the whole set is in #6665.

#skip-changelog

🤖 Generated with Claude Code

Replace catch (Throwable) with the exceptions the guarded code can throw, so SDK bugs and fatal errors at these sites are no longer swallowed. Most are RejectedExecutionException around executor submit and schedule, IOException or SecurityException around file I/O, and the specific parsing exceptions.

Several tests only passed because these catches hid mocks that don't behave like the real objects. They now do.

Refs #5513

Refs JAVA-552

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@linear-code

linear-code Bot commented Nov 18, 2026

Copy link
Copy Markdown

JAVA-552

@github-actions

Copy link
Copy Markdown
Contributor

🚨 Detected changes in high risk code 🚨

High-risk code has higher potential to break the SDK and may be hard to test. To prevent severe bugs, apply the rollout process for releasing such changes and be extra careful when changing and reviewing these files:

  • sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sentry

sentry Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.64.0 (1) release

⚙️ sentry-android Build Distribution Settings

runningcode and others added 2 commits November 18, 2026 18:34
The fixes to existing tests whose mocks only passed behind broad catches stay.

Refs #5513

Refs JAVA-552

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 18, 2026
SentryEnvelopeItem.fromClientReport only throws IOException. The name is fully qualified so this PR doesn't touch the import block, where #6665 adds ExceptionUtils right next to it.

Refs #5513

Refs JAVA-552

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 18, 2026

This branch has not been deployed

No deployments
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.

1 participant