Home > DeveloperSection > Forums > How to create a custom exception in c#
ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    June-13-2013 3:23 AM

 C# C# 
Ratings:


 2 Reply(s)
 1165  View(s)
Rate this:

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.



Vijay Shukla

Total Post:100

Points:700
Posted on    June-13-2013 6:50 AM

Hello  ezra heywood!

You need to throw your custom exception 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 exception
void CallUpdateDatabase()
{
 try
  {
    UpdateDatabase();
  }
  catch(DatabaseException dbEx)
  {
    // Handle your custom exception
  }
}

rampt patter
rampt patter

Total Post:1

Points:7
Posted on    February-17-2015 10:15 PM

sample custom exception..

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

Rampt

 

 


Modified On Feb-19-2015 01:30:39 AM

Don't want to miss updates? Please click the below button!

Follow MindStick