
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Infinity or Exception in C# when divide by 0?
Divide by zero is the System.DivideByZeroException, which is a class that handles errors generated from dividing a dividend with zero.
Let us see an example.
Example
using System; namespace ErrorHandlingApplication { class DivNumbers { int result; DivNumbers() { result = 0; } public void division(int num1, int num2) { try { result = num1 / num2; } catch (DivideByZeroException e) { Console.WriteLine("Exception caught: {0}", e); } finally { Console.WriteLine("Result: {0}", result); } } static void Main(string[] args) { DivNumbers d = new DivNumbers(); d.division(25, 0); Console.ReadKey(); } } }
Output
Exception caught: System.DivideByZeroException: Attempted to divide by zero. at ErrorHandlingApplication.DivNumbers.division (System.Int32 num1, System.Int32 num2) [0x00000] in <a9b37148b4814c1a849bf4ee94fbe889> :0 Result: 0
The values entered here is num1/ num2.
result = num1 / num2;
Above, if num2 is set to 0, then the DivideByZeroException is caught since we have handled exception.
- Related Articles
- Infinity or exception in Java when divide by 0?
- Handling the Divide by Zero Exception in C++
- How to capture divide by zero exception in Java?
- How to capture divide by zero exception in C#?
- How to divide matrix rows by maximum value in each row excluding 0 in R?
- Divide the expression$\frac{523}{0}$
- How to divide data.table object rows by maximum value in each row excluding 0 in R?
- How to divide data frame row values by maximum value in each row excluding 0 in R?
- Divide 4170 by 60.
- Divide:13.455 by 4.14.
- Divide 254 by 8.
- Divide:10 by 10
- Divide: 24.505 by 0.05
- Divide 58219 by 365.
- Divide 9867 by 1000.

Advertisements