
- 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
How to capture divide by zero exception in C#?
System.DivideByZeroException is a class that handles errors generated from dividing a dividend with zero.
Example
Let us see an 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
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 above.
- Related Articles
- How to capture divide by zero exception in Java?
- Handling the Divide by Zero Exception in C++
- How to capture an exception raised by a Python Regular expression?
- How to capture null reference exception in C#?
- How to catch a divide by zero error in C++?
- How to capture file not found exception in Java?
- How to capture file not found exception in C#?
- How to capture out of memory exception in C#?
- How to capture and print Python exception message?
- How to capture index out of range exception in C#?
- Infinity or Exception in C# when divide by 0?
- Infinity or exception in Java when divide by 0?
- How to capture out of array index out of bounds exception in Java?
- How to capture screenshots in Selenium?
- How to capture SIGINT in Python?

Advertisements