11

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
        {
        }
    }
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79

11 Answers11

17
int percent = 0
if (max != 0) percent = (100*position) / max
rjzii
  • 14,236
  • 12
  • 79
  • 119
Simon
  • 78,655
  • 25
  • 88
  • 118
9

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.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
9
  • 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.

Swati
  • 50,291
  • 4
  • 36
  • 53
3

Check for zero.

if ( max == 0 ) {
    txt = "0%";
} else {
    // Do the other stuff....
palehorse
  • 26,407
  • 4
  • 40
  • 48
2

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.

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
1

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
tzot
  • 92,761
  • 29
  • 141
  • 204
0

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

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • I have to disagree here. Allowing the exception to be thrown comes with a performance hit - what if the calculation is happening in a large loop sequence? If we can anticipate the problem, then we should react to it BEFORE the exception is thrown, and leave the exception handling to unknown scenarios. – Patrick McCurley Jun 16 '12 at 18:44
  • @PatrickMcCurley This isn't a comment on generic arithmetic exception handling - it's an answer to the actual question asked. – Marcin Jun 16 '12 at 20:43
0

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.

Bob King
  • 25,372
  • 6
  • 54
  • 66
0

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.

ckramer
  • 9,419
  • 1
  • 24
  • 38
0

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 *

Dre
  • 4,298
  • 30
  • 39
0

You can user a ternary operator.

int percent = max != 0 ? (100 * position) / max : 0;

This means that when max does not equal zero, to perform the calculation. If it equals 0 then it will set the percent to 0.

Stephen85
  • 250
  • 1
  • 15