

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- How to divide data.table object rows by maximum value in each row excluding 0 in R?
- MySQL IF/WHEN/ELSE/OR with ORDER BY FIELD
- How to divide data frame row values by maximum value in each row excluding 0 in R?
- Divide one polynomial by another in Python
- Test element-wise for positive or negative infinity in Numpy
- Test array values for positive or negative infinity in Numpy
- What happens when a String is accepted or rejected by NPDA?
- How to divide a string by line break or period with Python regular expressions?
- Divide one Hermite series by another in Python
- Divide one Laguerre series by another in Python
Advertisements