HOW TO CREATE A CUSTOM EXCEPTION IN C#

ezra heywood

Total Post:145

Points:1019
Posted by  ezra heywood
C# 
C#
 1491  View(s)
Ratings:
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.

  1. Vijay Shukla

    Post:100

    Points:700
    Re: How to create a custom exception in c#

    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
      }
    }

  1. rampt patter

    Post:1

    Points:7
    Re: How to create a custom exception in c#

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!