How to create a custom exception in c#
I want to create my own exception in windows form application. I am trying to add some data into the database.
code:
try
{
string insertData = string.Format("INSERT INTO " + co开发者_运维百科nstants.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 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 shows 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 don't know whether which I created custom exception handling is right or wrong.
Can any one help me? Thanks in advance.
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
}
}
None of the SqlCommand properties and methods throw the DatabaseException you've created. Therefore, your catch will never trigger.
The fact that you've created and called your Exception DatabaseException does not mean that all "database" code will now throw your exception. When SqlCommand was written, it was written to throw a very specific set of exceptions. You can find the list of exceptions SqlCommand and its methods throw over at MSDN.
Let me know if my answer doesn't make sense to you.
your own exception is fine but Sql.ExecuteQuery will throw an SqlException, you could do something like this:
void CallDatabase( /* your parameters */)
{
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 (SqlException ex)
{
throw new DataBaseException("Database error", ex);
}
}
/* somewhere in your code */
try
{
CallDatabase(...);
}
catch (DataBaseException ex)
{
MessageBox.Show(ex.Message);
}
This is how I would write this routine (more or less). Make sure that you close the DB connection and use the using construct to free up the sqlCommand obj:
try
{
string insertData = string.Format("INSERT INTO " + constants.PIZZABROADCASTTABLE + " VALUES(@starttime,@endtime,@lastupdatetime,@applicationname)");
using (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 (Exception ex)
{
string s = "Failed to insert into table " + constants.PIZZABROADCASTTABLE + "Database Error! " + Environment.NewLine + "Details: " + ex.ToString();
MessageBox.Show(s, MessageBoxButtons.OK, MessageBoxIcons.Error);
// Or
//throw new DatabaseException(s, ex);
}
finally
{
if (databaseConnectivity != null && databaseConnectivity.connection != null)
databaseConnectivity.connection.Close();
else
MessageBox.Show("No database connectivity!", "Error", MessageBoxButtons.OK, MessageBoxIcons.Error);
}
With Throw Class , more information Here.
http://msdn.microsoft.com/en-us/library/system.activities.statements.throw.aspx
Regards.
精彩评论