Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Infinity or Exception in C# when divide by 0?
When dividing by zero in C#, the behavior depends on the data type being used. Integer division by zero throws a DivideByZeroException, while floating-point division by zero returns Infinity or NaN (Not a Number) without throwing an exception.
Syntax
Following is the basic division syntax that can result in divide-by-zero scenarios −
result = dividend / divisor;
Exception handling syntax for integer division −
try {
result = dividend / divisor;
} catch (DivideByZeroException ex) {
// handle exception
}
Integer Division by Zero - Exception
When dividing integers by zero, C# throws a DivideByZeroException at runtime −
Example
using System;
class Program {
public static void Main() {
int num1 = 10;
int num2 = 0;
try {
int result = num1 / num2;
Console.WriteLine("Result: " + result);
} catch (DivideByZeroException ex) {
Console.WriteLine("Exception caught: " + ex.Message);
}
Console.WriteLine("Program continues...");
}
}
The output of the above code is −
Exception caught: Attempted to divide by zero. Program continues...
Floating-Point Division by Zero - Infinity
Floating-point numbers (float, double) follow IEEE 754 standard and return special values instead of throwing exceptions −
Example
using System;
class Program {
public static void Main() {
double positiveNum = 10.0;
double negativeNum = -10.0;
double zero = 0.0;
double divisor = 0.0;
Console.WriteLine("Positive / Zero: " + (positiveNum / divisor));
Console.WriteLine("Negative / Zero: " + (negativeNum / divisor));
Console.WriteLine("Zero / Zero: " + (zero / divisor));
// Check for infinity
double result = positiveNum / divisor;
Console.WriteLine("Is result infinity? " + Double.IsInfinity(result));
Console.WriteLine("Is result positive infinity? " + Double.IsPositiveInfinity(result));
}
}
The output of the above code is −
Positive / Zero: ? Negative / Zero: -? Zero / Zero: NaN Is result infinity? True Is result positive infinity? True
Comparison of Data Types
| Data Type | Division by Zero Behavior | Result |
|---|---|---|
| int, long, short, byte | Throws DivideByZeroException | Runtime exception |
| double, float | Returns special values | ±? or NaN |
| decimal | Throws DivideByZeroException | Runtime exception |
Using Decimal Division by Zero
Example
using System;
class Program {
public static void Main() {
decimal num1 = 10.5m;
decimal num2 = 0.0m;
try {
decimal result = num1 / num2;
Console.WriteLine("Result: " + result);
} catch (DivideByZeroException ex) {
Console.WriteLine("Decimal division exception: " + ex.Message);
}
}
}
The output of the above code is −
Decimal division exception: Attempted to divide by zero.
Checking for Special Values
Example
using System;
class Program {
public static void Main() {
double[] values = { 5.0 / 0.0, -5.0 / 0.0, 0.0 / 0.0, 10.0 / 2.0 };
foreach (double value in values) {
Console.WriteLine("Value: " + value);
Console.WriteLine(" Is Infinity: " + Double.IsInfinity(value));
Console.WriteLine(" Is NaN: " + Double.IsNaN(value));
Console.WriteLine(" Is Normal: " + (!Double.IsInfinity(value) && !Double.IsNaN(value)));
Console.WriteLine();
}
}
}
The output of the above code is −
Value: ? Is Infinity: True Is NaN: False Is Normal: False Value: -? Is Infinity: True Is NaN: False Is Normal: False Value: NaN Is Infinity: False Is NaN: True Is Normal: False Value: 5 Is Infinity: False Is NaN: False Is Normal: True
Conclusion
In C#, division by zero behavior depends on the data type: integer types throw DivideByZeroException, while floating-point types return Infinity or NaN. Understanding this distinction is crucial for proper error handling and mathematical computations in your applications.
