forum

Home / DeveloperSection / Forums / How to create a custom exception in c#

How to create a custom exception in c#

Anonymous User259213-Jun-2013

I want to create my own exception in windows form application.I am trying 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 @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.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 is right or wrong.can any one help me.Thanks in advance.


c#c# 
Updated on 17-Feb-2015
I am a content writter !

Can you answer this question?


Answer

2 Answers

Liked By