---
title: "How to create a custom exception in c#"  
description: "How to create a custom exception in c#"  
author: "Anonymous User"  
published: 2013-06-13  
updated: 2015-02-17  
canonical: https://www.mindstick.com/forum/1060/how-to-create-a-custom-exception-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to create a custom exception in c#

I want to create my own exception in [windows](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer) [form application](https://www.mindstick.com/interview/23108/how-to-create-form-application-using-dot-net).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 add the add some data into the database.

\
**code:**

\

```
 try        {             string insertData = string.Format("INSERT INTO " + constants.PIZZABROADCASTTABLE + " VALUES(@starttime,@endtime,@lastupdatetime,@applicationname)");            sqlCommand = new SqlCommand(insertData, databaseConnectivity.connection);            sqlCommand.Parameters.AddWithValue("@starttime", broadcastStartDateTime);            sqlCommand.Parameters.AddWithValue("@endtime", broadcastEndDateTime);            sqlCommand.Parameters.AddWithValue("@lastuptime", currentDateTime);            sqlCommand.Parameters.AddWithValue("@applicationname", txtApplicationName.Text);            sqlCommand.ExecuteNonQuery();        }        catch (DataBaseException ex)        {            MessageBox.Show(ex.Message);        }
```

\

here I have created a my own exception.Here I have given scalar [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) @lastuptime instead of @lastupdatetime for capturing the sqlexception.

here is my databaseException class.

```
class DataBaseException : Exception{    public DataBaseException(string Message)        : base(Message)    {     }    public DataBaseException(string message, Exception innerException)        : base(message, innerException)    {    }}
```

\

Here while running the program it show the error at

[sqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder).ExecuteQuery();

but it does not capture the error and show the message box text.I know i have done some thing wrong.I do know whether which i created custom [exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) is [right or wrong](https://answers.mindstick.com/qa/99278/china-s-visit-to-russia-during-russo-ukrainian-war-is-right-or-wrong).can any one help me.Thanks in advance.

## Replies

### Reply by rampt patter

sample custom [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling)..

```
    public class MyCustomException : System.Exception    {            public MyCustomException() : base() {}            public MyCustomException(string message): base(message)            {                MessageBox.Show(message);            }    }
```

Rampt

### Reply by Vijay Shukla

Hello ezra heywood!

You need to throw your [custom exception](https://www.mindstick.com/forum/159237/how-to-create-a-custom-exception-in-java-and-when-would-to-use-it) so that it can be caught by the calling method. In your code, program will throw exception from the DB and not your custom exception.

```
void UpdateDatabase(){//...        try            {             }               // Your checks to identify - type of exception               // ...              // .net exception              // Throw your custom exception in the appropriate block -               // throw new DatabaseException();            catch (OutOfMemoryException ex1)            {             }            catch (InvalidDataException ex2)            {             }            // Generic exception            catch (Exception ex3)            {                // throw new DatabaseException();            }//....} // Method calling UpdateDatabase need to handle Custom exceptionvoid CallUpdateDatabase(){try  {    UpdateDatabase();  }  catch(DatabaseException dbEx)  {    // Handle your custom exception  }}
```


---

Original Source: https://www.mindstick.com/forum/1060/how-to-create-a-custom-exception-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
