C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We discuss issues related to the compiler system and dividing a number by zero.
Info: The division is evaluated to 100 / 0, which cannot be done by the execution engine. The program terminates by throwing a DivideByZeroException.
Tip: To avoid the exception, you must always test the denominator value for zero. Alternatively you can use try and catch.
TryCatchC# program that divides by zero
using System;
class Program
{
static void Main()
{
//
// This expression evaluates to 100 / 0, which throws.
// ... You can check the denominator in a separate step.
//
int result = 100 / int.Parse("0");
Console.WriteLine(result);
}
}
Output
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
at Program.Main() in ... Program.cs:line 11
Error 1:
Division by constant zero
Tip: To avoid the divide by zero exception, you obviously must check the denominator against the zero value.
And: This numeric check will be much faster than having an exception thrown in the runtime.