diff --git a/app/src/main/java/com/example/vu/android/MainActivity.java b/app/src/main/java/com/example/vu/android/MainActivity.java index 340a6369..c07e2e85 100644 --- a/app/src/main/java/com/example/vu/android/MainActivity.java +++ b/app/src/main/java/com/example/vu/android/MainActivity.java @@ -53,12 +53,19 @@ protected void onCreate(Bundle savedInstanceState) { int t = 5 / 0; }); - // Unhandled - NegativeArraySizeException + // Handled - invalid array size is reported instead of crashing with NegativeArraySizeException Button negative_index_button = findViewById(R.id.negative_index); negative_index_button.setOnClickListener(view -> { addAttachment(false); Sentry.addBreadcrumb("Button for NegativeArraySizeException clicked..."); - int[] a = new int[-5]; + + int requestedSize = -5; + if (requestedSize < 0) { + Sentry.captureException( + new IllegalArgumentException("Invalid array size requested: " + requestedSize)); + return; + } + int[] a = new int[requestedSize]; }); // Handled - ArrayIndexOutOfBoundsException