---
title: "What is Exception Handling in C#?"  
description: "What is Exception Handling in C#?"  
author: "Anonymous User"  
published: 2018-12-13  
updated: 2018-12-13  
canonical: https://www.mindstick.com/forum/34716/what-is-exception-handling-in-c-sharp  
category: "c#"  
tags: ["c#", "asp.net", "api(s)", "oops", "mvc", "oop"]  
reading_time: 5 minutes  

---

# What is Exception Handling in C#?

[Please tell me](https://www.mindstick.com/forum/33900/please-tell-me-what-are-the-technique-to-content-optimization) everything in brief.\

## Replies

### Reply by Anonymous User

## Exception Handling in C#

The Exception Handling in C# is a process to handle runtime errors. You should perform exception handling so that normal flow of the application can be maintained even after runtime errors. The exception is a problem that arises during the execution of a program. The C# exception is a response to an exceptional situation that arises while a program is running, such as an attempt to divide by zero. An Exceptions provide a way to transfer control from one part of a program to another. The C# exception handling is built upon four keywords: try, catch, finally, and throw.

**Try** − The try block identifies a block of code for which particular exceptions is activated. This is followed by one or more catch blocks.

**Catch** – A C# program catches an exception with an exception handler at the place in a program where you want to handle the problem. That is the catch keyword indicates the catching of an exception.

**Finally** − A finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. By an example, if you open a file, it must be closed whether an exception is raised or not.

**Throw** – A C# program throws an exception when a problem shows up. This is done using a throw keyword. We can throw an object if it is either directly or indirectly derived from the System. Exception class. We can use a throw statement in the catch block to throw the present object as –

```
Catch (Exception e) {
   ...
   Throw e
}
```

```
//C#: Exception Handling:
using System;class Customer{    public static void Main()    {        try        {            throw new DivideByZeroException("Invalid Division");        }
        catch (DivideByZeroException)        {            Console.WriteLine("Exception");        }
        Console.WriteLine("LAST STATEMENT");    }  }
```

**Note: -** A try block may have multiple catch block.

The Exception handling is an inbuilt mechanism in the .NET framework to detect and handle run-time errors. The .NET framework contains a lot of standard exceptions. And the exceptions are anomalies that occur during the execution of a program. That can be because of the user, logic or system errors. If the programmer (user) does not provide a mechanism to handle these anomalies, the .NET runtime environment provides a default mechanism, which terminates the program execution.

```
Example - 1

using System;using System.IO;
class FinallyDemo{    static void Main(string[] args)    {        FileStream outStream = null;        FileStream inStream = null;
        try        {            outStream = File.OpenWrite("DestinationFile.txt");            inStream = File.OpenRead("BogusInputFile.txt");        }        catch(Exception ex)        {            Console.WriteLine(ex.ToString());        }
        finally        {            if (outStream != null)            {                outStream.Close();                Console.WriteLine("outStream closed.");            }            if (inStream != null)            {                inStream.Close();                Console.WriteLine("inStream closed.");            }        }    }}
```

```
Example - 2

using System;namespace ExceptionHandlingApplication{
    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();        }    }}
```

![What is Exception Handling in C#?](https://www.mindstick.com/mindstickforums/222ba611-d779-47b3-adea-648d98db30de/images/2c00a1c2-734f-443f-91e3-aabbe7a99e8b.png)\

| Namespace in C# for ExceptionHandling :- **1.** System.OutOfMemoryException \ **2.** System.NullReferenceException \ **3.** Syste.InvalidCastException \ **4.** Syste.ArrayTypeMismatchException \ **5.** System.IndexOutOfRangeException \ **6.** System.ArithmeticException \ **7.** System.DevideByZeroException \ **8.** System.OverFlowException |
| --- |

\

**Thank You for Reading this article...and If any suggestion for me, please comment me by the comment box.**


---

Original Source: https://www.mindstick.com/forum/34716/what-is-exception-handling-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
