Divide by zero error, how do I fix this?

百般思念 提交于 2019-12-10 02:12:05

问题


C# novice here, when the int 'max' below is 0 I get a divide by zero error, I can see why this happens but how should I handle this when max is 0? position is also an int.

    private void SetProgressBar(string text, int position, int max)
    {
        try
        {
            int percent = (100 * position) / max; //when max is 0 bug hits
            string txt = text + String.Format(". {0}%", percent);
            SetStatus(txt);
        }
        catch
        {
        }
    }

回答1:


int percent = 0
if (max != 0) percent = (100*position) / max



回答2:


Well, that entirely depends on the behaviour you want. If the maximum value of your program bar is zero, is it full? Is it empty? This is a design choice, and when you've chosen, just test for max == 0 and deploy your answer.




回答3:


  • You can throw an exception.
  • You can do int percent = ( max > 0 ) ? (100 * position) / max : 0;
  • You can choose to do nothing instead of assigning a value to percent.
  • many, many other things...

Depends on what you want.




回答4:


Check for zero.

if ( max == 0 ) {
    txt = "0%";
} else {
    // Do the other stuff....



回答5:


This is not a C# problem, it's a math problem. Division by zero is undefined. Have an if statement that checks whether max > 0 and only execute your division then.




回答6:


Convert your

int percent = (100 * position) / max;

into

int percent;
if (max != 0)
    percent = (100 * position) / max;
else
    percent = 100; // or whatever fits your needs



回答7:


Well, if max is zero, then there is no progress to be made. Try catching the exception where this is called. That is probably the place to decide whether there is a problem or if the progress bar should be set at zero or at 100%.




回答8:


I guess the root question is: Does it make sense to even call this function where max is '0'? If yes, then I'd add special handling to it i.e.:

if (max == 0) 
{
    //do special handling here
}
else
{
    //do normal code here
}

If 0 doesn't make sense, I'd investigate where it's coming from.




回答9:


You would need a guard clause which checks for max == 0.

private void SetProgressBar(string text, int position, int max)
{
    if(max == 0)
        return;
    int percent = (100 * position) / max; //when max is 0 bug hits
    string txt = text + String.Format(". {0}%", percent);
    SetStatus(txt);
}

You could also handle the Divide by Zero exception, as your sample showed, but it is generally more costly to handle exceptions then to set up checks for known bad values.




回答10:


If you are using this for a download, you'll probably want to show 0% as I assume max would == 0 in this case when you don't KNOW the file size yet.

int percent = 0;
if (max != 0)
    ...;

If you are using this for some other long task, I'd want to assume 100%

But also, since position can never be between 0 and -1, so you'll probably want to drop the 100 *



来源:https://stackoverflow.com/questions/160141/divide-by-zero-error-how-do-i-fix-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!