---
title: "Exception Class in C#"  
description: "In this blog I am trying to explain the concept of the Exception in C#.Exception ClassException Class is the base class for all exceptions. When an er"  
author: "Vijay Shukla"  
published: 2013-06-17  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/534/exception-class-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Exception Class in C#

In this blog I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to [explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of the [Exception in C#](https://www.mindstick.com/forum/158619/what-is-an-exception-in-c-sharp-and-how-does-it-relate-to-error-handling-in-asp-dot-net-mvc).

Exception Class

Exception Class is the [base class](https://www.mindstick.com/blog/116/base-class-initilization) for all [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions). When an error occurs, either the system or the currently executing [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) reports it by throwing an exception containing information about the error. Once thrown, an exception is handled by the application or by the default exception handler.

Try-Catch Blocks

The [common language runtime](https://www.mindstick.com/articles/16/common-language-runtime) (CLR) provides an [exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) model that is based on the representation of exceptions as objects, and the separation of [program code](https://www.mindstick.com/interview/33814/what-will-be-the-output-of-following-program-code) and exception handling code into try blocks and catch blocks, respectively. There can be one or more catch blocks, each designed to handle a particular type of exception, or one block designed to catch a more specific exception than another block.

If an application handles exceptions that occur during the execution of a block of application code, the code must be placed within a try statement. Application code within a try statement is a try block. Application code that handles exceptions thrown by a try block is placed within a catch statement, and is called a [catch block](https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block). Zero or more catch blocks are associated with a try block, and each catch block includes a type filter that determines the types of exceptions it handles.

##### Example: -

```
 using System; class ExceptionClass {
 public static void Main() { int x = 0; try { int y = 100 / x; } catch (ArithmeticException e) { Console.WriteLine("ArithmeticException Handler: {0}", e.ToString()); } catch (Exception e) { Console.WriteLine("Generic Exception Handler: {0}", e.ToString()); } Console.ReadKey(); }
 }
```

##### Code Description: -

1. Using System (include the System namespace).

2. Create ExceptionClass class.

4. Create Main method.

5. Declare x named variable.

7. Start try block.

9. Write the statement in try block.

11. Start catch block.

13. Write the message on console from e variable.

And below code work same as 11 and 13 statement.

##### Output: -

![Exception Class in C#](https://www.mindstick.com/blogs/c725c918-05a4-4383-bbf0-ce506c9a3d6b/images/fef57e35-0f33-4b8c-88fd-f244533e44e3.png)

---

Original Source: https://www.mindstick.com/blog/534/exception-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
