Open In App

C# Exceptions

Last Updated : 20 Sep, 2025
Suggest changes
Share
Like Article
Like
Report

In C#, an exception is an unexpected event that occurs during the execution of a program, which disrupts the normal flow of the program. Exceptions can occur due to various reasons such as invalid input, division by zero, file not found, or network issues.

Example:

C#
int a = 5, b = 0; int result = a / b; // Throws DivideByZeroException Console.WriteLine(result); 

Types of Exceptions in C#

ExceptionClassHierarchy-1
Types of Exception in C#

Exceptions in C# can be broadly classified into two types:

1. System Exceptions

  • These are exceptions thrown by the CLR (Common Language Runtime).
  • Examples: DivideByZeroException, NullReferenceException, IndexOutOfRangeException

2. Application Exceptions

  • These are custom exceptions defined by the user to indicate application-specific errors.
  • Derived from the ApplicationException class.

Examples of Common Exceptions

1. DivideByZeroException

Occurs when a number is divided by zero.

C#
int a = 10, b = 0; int result = a / b; // Throws DivideByZeroException 

2. NullReferenceException

Occurs when trying to access a member of an object that is null.

C#
string str = null; int len = str.Length; // Throws NullReferenceException 

3. IndexOutOfRangeException

Occurs when accessing an array element outside its valid range (size of array).

C#
int[] arr = { 1, 2, 3 }; int x = arr[5]; // Throws IndexOutOfRangeException 

4. Custom Application Exception

You can define your own exceptions by extending ApplicationException.

C#
using System; class InvalidAgeException : ApplicationException {  public InvalidAgeException(string message) : base(message) { } } class Program {  static void CheckAge(int age)  {  if (age < 18)  throw new InvalidAgeException("Age must be 18 or older.");  } } 

Properties of the Exception Class

The Exception class in C# provides several properties to retrieve detailed information about an exception when it occurs:

  • Data: Holds arbitrary user-defined information in key-value pairs related to the exception.
  • TargetSite: Returns the method where the exception was thrown.
  • Message: Provides a description of the cause of the exception.
  • HelpLink: Stores a URL pointing to a help file or documentation for the exception.
  • StackTrace: Provides the sequence of method calls that led to the exception, helping to locate where the error occurred.
  • InnerException: Contains another exception that caused the current exception, allowing tracking of exception chains.

Exception vs Error

FeatureExceptionError
DefinitionUnexpected condition in program flowSerious problem that cannot be handled in normal program flow
RecoverabilityCan be handled or recoveredUsually cannot be handled
OccurrenceHappens at runtimeHappens at compile-time or runtime (system-level)
ExamplesNullReferenceExceptionDivideByZeroExceptionStackOverflowErrorOutOfMemoryError
PurposeIndicates conditions that can be managedIndicates serious problems beyond program control

Key Points

  • Exceptions are runtime anomalies, not compile-time errors.
  • System exceptions are thrown by the CLR, application exceptions are user-defined.
  • Exception properties provide detailed information about the cause and context of the error.
  • Unlike errors, exceptions can often be anticipated and managed.

Explore