---
title: "Create custom (User Define) Exception in C#"  
description: "In this article I am trying to explain the concept of custom (User Define) Exception in C# and I demonstrate this concept with making a small demo.Use"  
author: "Vijay Shukla"  
published: 2013-06-13  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/527/create-custom-user-define-exception-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Create custom (User Define) Exception in C#

In this [article](https://www.mindstick.com/articles/95867/are-you-looking-for-500-credit-score-mortgage-lenders-houston-continue-reading-this-article) 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 custom (User Define) [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) and I demonstrate this concept with making a small demo.

User Define Exception

If you want users to be able to programmatically distinguish between some error conditions, you can create your own user-defined exceptions. The .NET [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library) provides a hierarchy of exception classes ultimately derived from the [base class](https://www.mindstick.com/blog/116/base-class-initilization) Exception. Each of these classes defines a specific exception, so in many cases you only have to catch the exception. You can also create your own exception classes by deriving from the Exception class.

##### Example: -

```
 using System;
 namespace MindstickCustomeExceptionDemo { public class MindstickEmployeeListNotFoundException : Exception { public MindstickEmployeeListNotFoundException() {
 } public MindstickEmployeeListNotFoundException(string message):base(message) {
 }
 } class CustomException { static void Main(string[] args) { try { throw new MindstickEmployeeListNotFoundException("\n\n::Mindstick Employee List not found::\n\n");
 } catch (MindstickEmployeeListNotFoundException ce) { Console.WriteLine(ce.ToString()); } Console.ReadKey(); } } }
```

##### Code Description: -

##### 1. Using System (include the System namespace).

##### 2. Create a namespace with MindstickCustomeExceptionDemo named.

##### 4. Create a public class with MindstickEmployeeListNotFoundException

##### named and inherit the Exception class.

##### \
6. Create a class constructor of MindstickEmployeeListNotFoundException

##### without parameter.

##### \
9. Create a class constructor of MindstickEmployeeListNotFoundException

##### with parameter.

##### \
13. Create a class with CustomException named.

##### \
15. Create Main method.

##### \

##### 17. Start try block.

##### \
19. Thrown the exception with its exception class name.

##### \
21. This catch block.

##### \
23. Print the exception message.

##### Output: -

If I pass any message in the form of string while thrown the exception then on console screen show passed string message.

In line number 19. I have pass throw new MindstickEmployeeListNotFoundException("\n\n::[Mindstick](https://yourviews.mindstick.com/view/84668/how-mindstick-is-used-for-marketing-purposes) [Employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks) List not found::\n\n"); then it message will show console screen.

##### ScreenShot:

![Create custom (User Define) Exception in C#](https://www.mindstick.com/blogs/71538d0a-c0d0-4499-ba4b-3990fed520ff/images/d5c4a480-c895-4775-a3fd-210473b68415.png)

In line number 19. I don't pass any message in string format such as: - throw new MindstickEmployeeListNotFoundException(); then it will show system message in console screen.

##### Screenshot: -

![Create custom (User Define) Exception in C#](https://www.mindstick.com/blogs/71538d0a-c0d0-4499-ba4b-3990fed520ff/images/15b157ca-c244-4245-8a82-bef13130cd2f.png)

---

Original Source: https://www.mindstick.com/blog/527/create-custom-user-define-exception-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
