Trending September 2023 # Types Of Exception In C# # Suggested October 2023 # Top 14 Popular | Dacquyenphaidep.com

Trending September 2023 # Types Of Exception In C# # Suggested October 2023 # Top 14 Popular

You are reading the article Types Of Exception In C# updated in September 2023 on the website Dacquyenphaidep.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Types Of Exception In C#

Introduction to Types of Exception in C#

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

public class check { public static void Main() { string val = new string('r', int.MaxValue); } }

Output:

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; class check { static void Main() { string value = null; if (value.Length == 0) { Console.WriteLine(value); } } }

Output:

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; class check { static void Main() { StringBuilder ref1 = new StringBuilder(); object ref2 = ref1; StreamReader ref3 = (StreamReader)ref2; } }

Output:

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:

class check { static void Main() { string[] arr1 = { "Welcome", "to", "CSharp" }; object[] arr2 = arr1; arr2[0] = 8; } }

Output:

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:

class check { static void Main() { int[] arr = new int[10]; arr[0] = 10; arr[10] = 20; arr[20] = 30; } }

Output:

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; class check { static void Main() { int res = 10 / int.Parse("0"); Console.WriteLine(res); } }

Output:

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; public class check { static void Recurse(int val) { Console.WriteLine(val); Recurse(++val); } public static void Main() { Recurse(0); } } Output:

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; class check { static void Main() { try { File.Open("D:\ex.txt", FileMode.Open); } catch (IOException) { Console.WriteLine("Inputoutput Exception is handled"); } } }

Output:

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.

Recommended Articles

This is a guide to Types of Exception in C#. Here we also discuss the Introduction and several types of exceptions in c# along with different examples and its code implementation.

You're reading Types Of Exception In C#

Update the detailed information about Types Of Exception In C# on the Dacquyenphaidep.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!