Home » Android » java – 'while' statement cannot complete without throwing an exception – Android

java – 'while' statement cannot complete without throwing an exception – Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

With this method I’m updating TextView every second.

 private void UpdatingTime(final String endTime, final long diffInDays) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(ONE_SECOND);
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    // Updating time every second
                                    long diffInHours = Methodes.diffInHours(endTime, diffInDays);
                                    long diffInMinutes = Methodes.diffInMinutes(endTime, diffInDays, diffInHours);
                                    long diffInSeconds = Methodes.diffInSeconds(endTime, diffInDays, diffInHours, diffInMinutes);

                                    tvTime2.setText(addZeroInFront(diffInHours)
                                            + ":" + addZeroInFront(diffInMinutes)
                                            + ":" + addZeroInFront(diffInSeconds));
                                }

                                private String addZeroInFront(long diffInHours) {
                                    String s = "" + diffInHours;
                                    if (s.length() == 1) {
                                        String temp = s;
                                        s = "0" + temp;
                                    }
                                    return s;
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }

This method is working perfect. But I got this warning:

‘while’ statement cannot complete without throwing an exception.

Reports for, while, or do statements which can only
exit by throwing an exception. While such statements may be correct,
they are often a symptom of coding errors.

I hate warnings and I want to fix it. How can i fix this warning, or there is a better solution for this infinite loop…?

How to&Answers:

This warning is a false positive; you should ignore it.

Usually, an infinite loop is a sign of a mistake somewhere; in your code, an infinite loop is exactly what you want.

Answer:

You are probably using Android Studio or IntelliJ.

If so, you can add this above your method containing the infinite loop to suppress warnings:

@SuppressWarnings("InfiniteLoopStatement")

Or add this “magic” comment above the statement:

//noinspection InfiniteLoopStatement

This will tell the IDE that this is ok.

More generally, when you get a false positive warning, do Alt+Enter and do what showed on the screenshot below (select class only if your class is full of false positive, for the same warning)

suppress warning image

More info here on Jetbrains help

Answer:

If you want something to fire every second you can follow this methodology where you are sure that it will remove the callback later when you leave. This calls itself every second until you are done with the activity. You could even remove the callbacks earlier if you want in the lifecycle such as within onPause() or onStop() if so desired.

Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) 
{
   super.onCreate(savedInstanceState);
   handler.post(updateTime);
}

@Override
protected void onDestroy() 
{
   super.onDestroy();
   handler.removeCallbacks(updateTime);
}


private final Runnable updateTime = new Runnable()
{
    public void run()
    {
        try 
        {
            // Updating time every second
            long diffInHours = Methodes.diffInHours(endTime, diffInDays);
            long diffInMinutes = Methodes.diffInMinutes(endTime, diffInDays, diffInHours);
            long diffInSeconds = Methodes.diffInSeconds(endTime, diffInDays, diffInHours, diffInMinutes);
            tvTime2.setText(addZeroInFront(diffInHours)
                                        + ":" + addZeroInFront(diffInMinutes)
                                        + ":" + addZeroInFront(diffInSeconds));

            handler.postDelayed(this, 1000);    
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }   
    }

    private String addZeroInFront(long diffInHours) 
    {
        String s = "" + diffInHours;
        if (s.length() == 1) 
        {
            String temp = s;
            s = "0" + temp;
        }
        return s;
    }
};

Answer:

You probably got this warning using IntelliJ IDEA, right?

As the others already mentioned, this warning can usually be ignored.

If you want to disable it, open Settings and go to Editor > Inspections and search for Infinite loop statement. Simply uncheck the box and you’re done.

And in general, you can simply place your text cursor over a warning you wish to disable and press Alt + Enter, then click on the inspection info field and click Disable inspection.