Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'while' statement cannot complete without throwing an exception - Android

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

like image 537
KiKo Avatar asked Jan 23 '15 16:01

KiKo


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

like image 130
SLaks Avatar answered Sep 21 '22 05:09

SLaks


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

like image 44
Louis CAD Avatar answered Sep 23 '22 05:09

Louis CAD


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.

like image 33
Ercksen Avatar answered Sep 21 '22 05:09

Ercksen


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;
    }
};
like image 40
Jay Snayder Avatar answered Sep 22 '22 05:09

Jay Snayder