Types of Exception in C#

PHPz
Release: 2024-09-03 15:20:36
Original
912 people have browsed it

The problem that arises during the execution of the program is an exception and these exceptions are the responses to circumstances that are exceptional during the running of a program like the exception raised whenever we are trying to divide by zero and the control is transferred from one part of the program to another part of the program through exceptions and the handling of exceptions is managed with four keywords in C#, they are try, catch, finally and throw blocks.

Types of Exception in C# with Examples

There are several types of exceptions in C#. They are:

1. System.OutOfMemoryException

The errors that are generated due to insufficient free memory is handled by this exception. Consider the below example program to demonstrate System. OutOfMemoryException.

Example:

//a class called check is defined
public class check
{
//main method is called
public static void Main()
{
// a string variable is created and tried to store 2.1 billion characters and this causes an out of memory exception
string val = new string('r', int.MaxValue);
}
}
Copy after login

Output:

Types of Exception in C#

Output:

In the above program, a class called check is defined. Then the main method is called. a string variable is created and tried to store 2.1 billion characters, and this causes an out of memory exception.

2. System.NullReferenceException

The errors that are generated from referencing a null object is handled by this exception. Consider the below example program to demonstrate System. NullReferenceException

Example:

using System;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
//a string variable is defined, and it is referencing to null
string value = null;
//the length of the value referencing to null is checked if it is equal to zero causing an exception
if (value.Length == 0)
{
Console.WriteLine(value);
}
}
}
Copy after login

Output:

Types of Exception in C#

In the above program, a class called check is defined. Then the main method is called. Then a string variable is defined, and it is referencing to null. Then the length of the value referencing to null is checked if it is equal to zero causing an exception.

3. System.InvalidCastException

The errors that are generated during typecasting is handled by this exception. Consider the below example program to demonstrate System. InvalidCastException.

Example:

using System.IO;
using System.Text;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
// an instance of the string builder class is created which is then assigned to a new object through implicit casting and then casting is tried explicitly to convert the instance of stringbuilder class to streamreader class
StringBuilder ref1 = new StringBuilder();
object ref2 = ref1;
StreamReader ref3 = (StreamReader)ref2;
}
}
Copy after login

Output:

Types of Exception in C#

In the above program, a class called check is defined. Then the main method is called. Then an instance of the string builder class is created which is then assigned to a new object through implicit casting and then casting is tried explicitly to convert the instance of stringbuilder class to streamreader class which causes an exception.

4. System.ArrayTypeMismatchException

The errors that are generated when there is a mismatch of type with the array type is handled by this exception. Consider the below example program to demonstrate System. ArrayTypeMismatchException.

Example:

//a class called check is defined
class check
{
//main method is called
static void Main()
{
// a string is defined and assigned the values which is then assigned to object class array and then an integer is tried to put in the same array which causes an exception
string[] arr1 = { "Welcome", "to", "CSharp" };
object[] arr2 = arr1;
arr2[0] = 8;
}
}
Copy after login

Output:

Types of Exception in C#

In the above program, a class called check is defined. Then the main method is defined. Then a string is defined and assigned the values which is then assigned to object class array and then an integer is tried to put in the same array which causes an exception.

5. System.IndexOutOfRangeException

The errors that are generated when a method is referring to an array that is out of range is handled by this exception. Consider the below example program to demonstrate System. IndexOutOfRangeException.

Example:

//a class called check is defined
class check
{
//main method is called
static void Main()
{
// an array is defined to store 100 integers but then an integer is tried to be stores at a position outside of the size of the array which causes an exception
int[] arr = new int[10];
arr[0] = 10;
arr[10] = 20;
arr[20] = 30;
}
}
Copy after login

Output:

Types of Exception in C#

In the above program, a class called check is defined. Then the main method is called. Then an array is defined to store 100 integers but then an integer is tried to be stored at a position outside of the size of the array which causes an exception.

6. System.DivideByZeroException

The errors that are generated when a dividend is divided by zero is handled by this exception. Consider the below example program to demonstrate System. DivideByZeroException.

Example:

using System;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
//an integer variable res is defined which is tried to divide by zero which causes an exception
int res = 10 / int.Parse("0");
Console.WriteLine(res);
}
}
Copy after login

Output:

Types of Exception in C#

In the above program, a class called check is defined. Then the main method is called. Then an integer variable res is defined which is tried to divide by zero which causes an exception.

7. System.StackOverflowException

The errors that are generated from stack overflowing is handled by this exception. Consider the below example program to demonstrate System. StackOverflowException.

Example:

using System;
//a class called check is defined
public class check
{
// a method called recurse is defined which takes a value as parameter and increases its value by one
static void Recurse(int val)
{
// since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing exception
Console.WriteLine(val);
Recurse(++val);
}
//main method is called
public static void Main()
{
//The recurse method is called to start the infinite recursion
Recurse(0);
}
} <strong>Output:</strong>
Copy after login

Types of Exception in C#

In the above program, a class called check is defined. Then a method called recurse is defined which takes a value as a parameter and increases its value by one. Then the main method is called in which the infinite loop for recursion begins by passing zero as a parameter. Then since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing an exception.

8. System.IO.IOException

The errors that are generated by input, the output is handled by this exception. Consider the below example program to demonstrate System. IO. IOException.

Example:

using System;
using System.IO;
//a class called check is defined
class check
{
//main methos is called
static void Main()
{
try
{
//a file is tried to open which do not exist and causes an exception
File.Open("D:\\ex.txt", FileMode.Open);
}
catch (IOException)
{
Console.WriteLine("Inputoutput Exception is handled");
}
}
}
Copy after login

Output:

Types of Exception in C#

In the above program, a class called check is defined. Then the main method is called. Then a file is tried to open which does not exist and causes an exception.

The above is the detailed content of Types of Exception in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template