Skip to content

Fix: Handle divide by zero in MainActivity#239

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/android-j9-divide-by-zero
Open

Fix: Handle divide by zero in MainActivity#239
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/android-j9-divide-by-zero

Conversation

@sentry

@sentry sentry Bot commented Jul 16, 2026

Copy link
Copy Markdown

This PR addresses the ArithmeticException: divide by zero occurring in MainActivity when the div_zero button is clicked (issue ANDROID-J9).

The root cause was a hardcoded int t = 5 / 0; operation within the button's onClickListener without any prior validation, leading to an unhandled crash.

The fix involves:

  • Replacing the direct 5 / 0 with a divisor variable.
  • Implementing a check to ensure the divisor is not zero before performing the division.
  • If the divisor is zero, a handled ArithmeticException is captured by Sentry, and a user-friendly Toast message is displayed, preventing the application from crashing.
  • Added the import android.widget.Toast; statement to MainActivity.java.

This change ensures the application handles the division by zero gracefully, aligning with the demo's purpose of showcasing both unhandled and handled exceptions.

Fixes ANDROID-J9

@sentry
sentry Bot requested a review from sdzhong as a code owner July 16, 2026 21:09
Comment on lines +54 to +60
int divisor = 0;
if (divisor == 0) {
Sentry.captureException(new ArithmeticException("divide by zero"));
Toast.makeText(MainActivity.this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
} else {
int t = 5 / divisor;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The divisor variable is hardcoded to 0, making the else branch containing the actual division unreachable dead code and contradicting the feature's stated purpose.
Severity: MEDIUM

Suggested Fix

To restore the intended behavior of demonstrating an unhandled exception, remove the if/else block and revert to the original code that causes a crash, such as int t = 5 / 0;. If the intent is to change this to a handled exception demo, update the UI labels and code comments to reflect this, and remove the dead code in the else branch.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: app/src/main/java/com/example/vu/android/MainActivity.java#L54-L60

Potential issue: The variable `divisor` is hardcoded to `0`. The subsequent `if (divisor
== 0)` check is therefore always true. This renders the `else` block, which contains the
division operation `int t = 5 / divisor;`, unreachable dead code. This change
contradicts the purpose of the button, which is intended to demonstrate an unhandled
`ArithmeticException` as indicated by UI labels and code comments. The code now always
executes the handled exception path, which is not the documented behavior for this
feature.

Did we get this right? 👍 / 👎 to inform future reviews.

@sentry

sentry Bot commented Jul 16, 2026

Copy link
Copy Markdown
Author

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
Android com.example.vu.android 24.12.26 (241226) release
Android com.example.vu.android 24.12.26 (241226) debug
Android com.example.vu.android 24.12.26 (241226) release

⚙️ android Build Distribution Settings

@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (fbd7a20) to head (4b221a4).

Files with missing lines Patch % Lines
...main/java/com/example/vu/android/MainActivity.java 0.00% 5 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main    #239   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         16      16           
  Lines        875     879    +4     
  Branches      65      66    +1     
=====================================
- Misses       875     879    +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

0 participants