19

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...?

Community
  • 1
  • 1
KiKo
  • 1,668
  • 7
  • 27
  • 56
  • 4
    you have an infinite loop (`while(true)`) and the only way to exit this loop is via exception. find another way to exit your while-loop, e.g. via a flag – Steffen Jan 23 '15 at 16:04
  • 1
    probably using a boolean instead of while(true) – Blackbelt Jan 23 '15 at 16:05
  • 1
    A boolean check that you then change within the lifecycle of the application would also suffice as pointed out by Blackbelt. – Jay Snayder Jan 23 '15 at 16:16
  • 1
    Is this a compiler warning, or is it a "warning" of the kind that Android Studio likes to litter the codebase with? By default, Android Studio will try to make numerous suggestions about alternative ways to write your code. – scriptocalypse Jan 23 '15 at 16:28

4 Answers4

45

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
37

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

Louis CAD
  • 10,965
  • 2
  • 39
  • 58
  • comment wont take effect without a space after "//". also, in my case what helped was `// noinspection InfiniteLoopJS` – Ivan Yulin Nov 30 '21 at 13:10
5

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.

Ercksen
  • 668
  • 9
  • 23
2

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;
    }
};
Jay Snayder
  • 4,298
  • 4
  • 27
  • 53