Last Updated : 11 Jul, 2025
An exception in C# is an unwanted or unexpected event that occurs at runtime. It affects the normal flow of the program. Common exceptions include invalid input, divide-by-zero operations, or accessing invalid array indices. C# provides a powerful exception-handling mechanism to successfully recover from these runtime issues and keep the program running smoothly.
Example: Divide By Zero Exception. This example shows the occurrence of the exception during divide by zero operation.
C#
// C# program to illustrate the exception
using System;
class Geeks {
static void Main(string[] args)
{
// Declaring two integer values
int A = 12;
int B = 0;
// divide by zero error
int C = A / B;
Console.Write("Value of C is " + C);
}
}
Output:
Unhandled Exception:Exception Handling in C#
System.DivideByZeroException: Attempted to divide by zero.
at Geeks.Main (System.String[] args) [0x00005] in <78d30a8ae7274e28ac400780dfde8dc7>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.DivideByZeroException: Attempted to divide by zero.
at Geeks.Main (System.String[] args) [0x00005] in <78d30a8ae7274e28ac400780dfde8dc7>:0
C# provides try-catch and finally blocks to handle exceptions effectively.
Syntax:
try
{
// Code that may throw an exception
}
catch (ExceptionType ex)
{
// Handle the exception
}
finally
{
// Cleanup code (optional)
}
Example:
C#
using System;
class Geeks
{
static void Main()
{
try
{
int A = 10;
int B = 0;
// Attempting to divide by zero
int res = A / B;
Console.WriteLine("Result: " + res);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
Console.WriteLine("Execution completed.");
}
}
}
Error: Attempted to divide by zero. Execution completed.C# Exception Hierarchy
In C#, all exceptions are derived from the base class Exception, which is further divided into two main categories:
All exception classes in C# are ultimately derived from the Exception class, with SystemException containing standard system-related exceptions and ApplicationException allowing users to define their own specific exceptions. To know the difference between these, refer: System Level Exception Vs Application Level Exception in C#
Difference Between Errors and ExceptionFeatures
Errors
Exceptions
Definition
Errors are unexpected issues that may arise during computer program execution.
Exceptions are unexpected events that may arise during run-time.
Handling
Errors cannot be handled by the Program.
Exceptions can be handled using try-catch mechanisms.
Relationship
All Errors are exceptions.
All exceptions are not errors.
C# Exception ClassesThere are different kinds of exceptions which can be generated in C# program:
1. Divide By Zero exception: It occurs when the user attempts to divide by zero.
Example:
int result = 10 / 0; // Throws DivideByZeroException
2. NullReferenceException: Occurs when referencing a null object.
Example:
string str = null;
Console.WriteLine(str.Length); // Throws NullReferenceException
3. IndexOutOfRangeException: Thrown when accessing an invalid index in an array.
Example:
int[] arr = {1, 2, 3};
Console.WriteLine(arr[5]); // Throws IndexOutOfRangeException
4. OutOfMemoryException: Occurs when the program exceeds available memory.
5. StackOverflowException: Caused by infinite recursion.
Example:
Properties of the Exception Classvoid Recursive() => Recursive();
Recursive(); // Throws StackOverflowException
The Exception class has many properties which help the user to get information about the exception during the exception.
Important Points:
catch (DivideByZeroException ex) { /* Handle divide-by-zero */ }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4