---
title: "Exception Handling in C#"  
description: "Handling of error occurring atRun-time is known as Exception handling. Exception handling is a way to preventapplication from crashing.  C# provides t"  
author: "Uttam Misra"  
published: 2011-01-01  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/89/exception-handling-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Exception Handling in C#

Handling of error occurring atRun-time is [known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) [Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling). Exception handling is a way to preventapplication from crashing.\

##### C# provides three keywords to handleexceptions.

· try

· catch

· finally

##### The try block

The lines of code which we think cangenerate error; we put them in try block. If exception is raised codecompilation is transferred to [catch block](https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block).

The catch block

When [exception occurs](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs) codecompilation is transferred to catch block and code inside catch block isexecuted. We can have more than one catch block for one try block.

##### The finally block

[Finally block](https://www.mindstick.com/articles/12106/exception-handling-in-java-guidelines-on-the-use-of-finally-block) is executed after tryblock regardless of the occurrence of error. Finally block contains all cleanupcodes. For example, if there is no need of open [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) to the databaseafter try block we can write code for connection close in finally block.

##### Example

```
try           {               System.Data.SqlClient.SqlCommandBuilder cb;               cb = new System.Data.SqlClient.SqlCommandBuilder(da);               DataRow dr = dt.Rows[count];               dr[0] = txtId.Text;               dr[1] = txtName.Text;               dr[2] = txtAddress.Text;               da.Update(ds, "Employee");               MessageBox.Show("DataUpdates");           }
```

\

Catch block will catch any erroroccurred in try block and [display message](https://www.mindstick.com/forum/12797/how-to-show-display-message-box-in-c-sharp-asp-dot-net-forms)

box with exception detail.

```
           catch (Exceptionex)           {               MessageBox.Show(ex.Message);           }
```

\

Finally block will be executedregardless of error occurred or not.

```
           finally           {               con.Close();           }
```

---

Original Source: https://www.mindstick.com/blog/89/exception-handling-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
