---
title: "Exception Handling in C#."  
description: "In this blog I will tell you that how to use exception handling mechanism in c# as well as here I will describe try, catch, finally and throw keyword."  
author: "Anonymous User"  
published: 2011-06-13  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/191/exception-handling-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Exception Handling in C#.

In this blog I will told you that how to [implement exception](https://www.mindstick.com/forum/33523/how-to-implement-exception-handling-in-mvc) [handling mechanism](https://www.mindstick.com/forum/160113/how-does-the-api-s-global-exception-handling-mechanism-work-and-when-is-it-useful) in c#. Here I will describe certain [keywords](https://www.mindstick.com/articles/12899/how-to-spot-if-you-re-optimizing-for-the-wrong-keywords) which are used while we implementing concept of [exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling).\

Firstly one thing remember that System.Exception is a [base class](https://www.mindstick.com/blog/116/base-class-initilization) for all the [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) which is generated in c#.net.We use following keywords while implementing concept of exception handling.\
1. try2. throw3. catch4. finally

**\**

We enclose a block of code that has the potential of throwing exception within a try block. A catch handler associated with the exception that is thrown catches the exception. There can be one or more catch handler for single try and each catch handler can be associated with a specific exception type that it can handle. [finally block](https://www.mindstick.com/articles/12106/exception-handling-in-java-guidelines-on-the-use-of-finally-block) contains that piece of code which will always executed in all cases whether exception is raised or not such as cleanup code or disconnection [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) from server. We use throw keyword for throwing exception at certain [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) which is caught by try block.

##### Syntax for exception handling

## \

```
try{            //Code that can encounters errors and raise exceptions.}catch{            //Code that handles errors and exceptions.}finally{            //Code that perform cleanup and executes both on normal execution            //path as well as in case of error occurs.}
```

Following example will demonstrate use of try-catch-finally block and throw keyword.

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ExceptionHandling{    class Program    {        static void Main(string[] args)        {            try            {                //Write down statements to generate exception.                Console.WriteLine("Here is demo of exception handling......!");                throw new Exception("Oops..! Exception is thrown....");            }            catch(Exception ex)            {                //Dispaly the error message.                Console.WriteLine("Caught Exception  :  {0}", ex.Message);            }            finally            {                 //This is a finally block which always executed.                Console.WriteLine("Ops Finally program end.");            }        }    }}
```

##### Output of following code snippet is as follows.

Here is demo of exception handling......!

Caught Exception: Oops..! Exception is thrown....

Ops finally program end.

---

Original Source: https://www.mindstick.com/blog/191/exception-handling-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
