Skip to content

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

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

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

Conversation

@runningcode

@runningcode runningcode commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

📜 Description

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

  • Executors (37): RejectedExecutionException around submit and schedule, which
    ISentryExecutorService declares.
    • SendCachedEnvelopeIntegration already caught it. Its broad arm is replaced by handling
      InterruptedException (restoring the interrupt flag) and ExecutionException from
      future.get(), next to the existing TimeoutException.
  • File I/O (23): IOException. SecurityException for File.delete, renameTo and
    exists, which can throw nothing else.
  • Parsing and the rest (20): the specific types: NumberFormatException,
    IllegalArgumentException, MalformedURLException, URISyntaxException,
    PatternSyntaxException, and so on.
    • Baggage's key/value parsing also catches IndexOutOfBoundsException, which an incoming entry
      without = triggers.

The 15 catches that guard nothing are removed in #6565 instead.

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:

The three PRs don't depend on each other. Baggage.java conflicts with #6565, which re-indents
the per-entry catch narrowed here, so whichever PR merges second needs a rebase.

Six sites the audit had marked for narrowing are left for the boundary PR:

  • DeviceInfoUtil.getExternalStorageStat calls context.getExternalFilesDirs, a framework call.
  • The Apollo 3 and 4 request-body writeTo runs the app's own upload code.
  • The view id lookup in ViewHierarchyEventProcessor is part of the walk over the app's views.
  • BuildInfoProvider.isEmulator and ContextUtils.getFamily read Build.* fields. Making them
    null-safe changes their signatures.

💚 How did you test it?

Ran the unit tests of sentry, sentry-android-core, sentry-android-ndk,
sentry-android-replay, sentry-async-profiler, sentry-apollo-3, sentry-apollo-4,
sentry-jdbc, sentry-opentelemetry-core and sentry-opentelemetry-otlp, 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

  • Add rethrowIfFatal and a comment saying why the catch is broad to the boundary sites, one PR
    per module group.

🤖 Generated with Claude Code

@linear-code

linear-code Bot commented Nov 11, 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

@sentry

sentry Bot commented Nov 11, 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 10:09
Replace catch (Throwable) with the types the guarded code can throw, so SDK bugs and fatal
errors are no longer swallowed: RejectedExecutionException around executor submit and
schedule, IOException and SecurityException around file I/O, and the specific parse
exceptions elsewhere. Drop the try where nothing in it can throw.

Several tests only passed because these catches hid broken mocks: executor mocks that
returned Unit from submit, mock Contexts without assets, a Runtime.exec that returned null.
Make those mocks behave like the real objects.

Refs JAVA-552

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@runningcode
runningcode force-pushed the no/java-552-narrow-throwable-catches branch from 93c9699 to 93ec984 Compare November 18, 2026 09:10
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 18, 2026
runningcode and others added 4 commits November 18, 2026 11:33
getTraceInputStream() is a Binder call into system_server, which can
rethrow any runtime exception from the other side. Narrowing its catch
to IOException let such an exception escape and abort the whole
exit-info pass. Catch broadly again, after rethrowIfFatal, so only that
ANR is skipped, as with a missing dump.

Refs JAVA-552

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ClassLoader.getResources belongs to the app or its container and can
throw unchecked exceptions, such as IllegalStateException for a closed
jar. With only IOException caught, loadModules threw, nothing was cached,
and MainEventProcessor failed on every event. Catch RuntimeException too,
so the modules found so far are returned and cached.

Refs JAVA-552

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When its queue is full, the executor returns a cancelled future instead
of throwing, and get() on it throws CancellationException. The removed
catch (Throwable) used to absorb it. Catch it explicitly, so register()
doesn't fail when the startup crash flush can't be queued.

Refs JAVA-552

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Debug's method tracing and Installation.id report failures with a plain
RuntimeException, so there is no narrower type to catch. Say so at each
catch, and what happens when it fails, as AGENTS.md asks for broad
catches.

Refs JAVA-552

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

Copy link
Copy Markdown
Contributor Author

Replaced by independent PRs against 9.x.x, one per kind of change (JAVA-552). This PR's narrowings and their test fixes are now in #6669, and the AnrV2Integration trace read boundary is in #6665.

@runningcode
runningcode deleted the no/java-552-narrow-throwable-catches branch November 18, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant