ube100
asked on
Nested exception handle
Hi, Have a look at this event handler:
private void TransactionButton_Click(ob ject sender, EventArgs e)
{
ClearDisplay();
StringWriter writeString = new StringWriter();
SqlTransaction tran = null;
SqlDataAdapter adapter = null;
using (SqlConnection connection = new System.Data.SqlClient.SqlC onnection(
Errors.Properties.Settings .Default.N orthwindCo nnectionSt ring))
{
try
{
adapter = new SqlDataAdapter();
// Reset the adapert's update commands using the new instance of the connection.
SetCommands(adapter, connection);
connection.Open();
tran = connection.BeginTransactio n(Isolatio nLevel.Rea dCommitted );
//Set the commands to use the transaction
adapter.UpdateCommand.Tran saction = tran;
adapter.InsertCommand.Tran saction = tran;
adapter.DeleteCommand.Tran saction = tran;
}
catch (Exception ex)
{
DisplayTextBox.Text = ex.Message;
// Early exit on error
return;
}
try
{
adapter.Update(dataSet.Tab les["Custo mers"]);
tran.Commit();
DisplayTextBox.AppendText( "No errors on update or Commit.");
}
catch (SqlException ex)
{
try
{
// Package up and display the errors
foreach (SqlError errSQL in ex.Errors)
{
writeString.WriteLine("Err or:");
writeString.WriteLine(" Message: {0}", errSQL.Message);
writeString.WriteLine(" Number: {0}", errSQL.Number);
writeString.WriteLine(stri ng.Empty);
}
DisplayTextBox.Text = writeString.ToString();
// Roll back the transaction.....
tran.Rollback();
}
catch (SqlException RollbackEx)
{
// Handle exception in rollback in case connection dies
foreach (SqlError err in RollbackEx.Errors)
{
writeString.WriteLine("Err or in Rollback:");
writeString.WriteLine(" Message: {0}", err.Message);
writeString.WriteLine(" Number: {0}", err.Number);
writeString.WriteLine(stri ng.Empty);
}
}
}
}
}
In this nested exception try - catch block where I'm trying to commit a transaction and for some reason the connection object momentarily lost the connection so the execution went into the nearest catch block. The next step is to loop through the error collection and print the error message, which it did and consequently part of that was to rollback the transaction. When it trying to do that since no connection available it should go into another catch block but it didn't and giving me the .net exception dialog for un-handled exception. Is there any way I could show the both of the error messages rather than giving the user that ugly .net exception dialog?
private void TransactionButton_Click(ob
{
ClearDisplay();
StringWriter writeString = new StringWriter();
SqlTransaction tran = null;
SqlDataAdapter adapter = null;
using (SqlConnection connection = new System.Data.SqlClient.SqlC
Errors.Properties.Settings
{
try
{
adapter = new SqlDataAdapter();
// Reset the adapert's update commands using the new instance of the connection.
SetCommands(adapter, connection);
connection.Open();
tran = connection.BeginTransactio
//Set the commands to use the transaction
adapter.UpdateCommand.Tran
adapter.InsertCommand.Tran
adapter.DeleteCommand.Tran
}
catch (Exception ex)
{
DisplayTextBox.Text = ex.Message;
// Early exit on error
return;
}
try
{
adapter.Update(dataSet.Tab
tran.Commit();
DisplayTextBox.AppendText(
}
catch (SqlException ex)
{
try
{
// Package up and display the errors
foreach (SqlError errSQL in ex.Errors)
{
writeString.WriteLine("Err
writeString.WriteLine(" Message: {0}", errSQL.Message);
writeString.WriteLine(" Number: {0}", errSQL.Number);
writeString.WriteLine(stri
}
DisplayTextBox.Text = writeString.ToString();
// Roll back the transaction.....
tran.Rollback();
}
catch (SqlException RollbackEx)
{
// Handle exception in rollback in case connection dies
foreach (SqlError err in RollbackEx.Errors)
{
writeString.WriteLine("Err
writeString.WriteLine(" Message: {0}", err.Message);
writeString.WriteLine(" Number: {0}", err.Number);
writeString.WriteLine(stri
}
}
}
}
}
In this nested exception try - catch block where I'm trying to commit a transaction and for some reason the connection object momentarily lost the connection so the execution went into the nearest catch block. The next step is to loop through the error collection and print the error message, which it did and consequently part of that was to rollback the transaction. When it trying to do that since no connection available it should go into another catch block but it didn't and giving me the .net exception dialog for un-handled exception. Is there any way I could show the both of the error messages rather than giving the user that ugly .net exception dialog?
You didn't happen to mention what kind of exception you are seeing in the nested t/c. If it is some kind of network exception or InvalidOperation, anything other than a SqlException, you wouldn't see it in the inner catch. Double check your exception's type. (If you do find it is not a SqlException add another catch for that type instead of breaking your own rules by catching an Exception.)
ASKER
This is what they doing:
try
{
adapter.Update(dataSet.Tab les["Custo mers"]);
tran.Commit();
DisplayTextBox.AppendText( "No errors on update or Commit.");
}
catch (SqlException ex)
{
try
{
// Package up and display the errors
foreach (SqlError errSQL in ex.Errors)
{
writeString.WriteLine("Err or:");
writeString.WriteLine(" Message: {0}", errSQL.Message);
writeString.WriteLine(" Number: {0}", errSQL.Number);
writeString.WriteLine(stri ng.Empty);
}
DisplayTextBox.Text = writeString.ToString();
// Roll back the transaction.....
tran.Rollback();
}
catch (SqlException RollbackEx)
{
// Handle exception in rollback in case connection dies
foreach (SqlError err in RollbackEx.Errors)
{
writeString.WriteLine("Err or in Rollback:");
writeString.WriteLine(" Message: {0}", err.Message);
writeString.WriteLine(" Number: {0}", err.Number);
writeString.WriteLine(stri ng.Empty);
}
}
Both instances they are catching SqlException? So say, when something happens just before commiting the transaction that obiviously will fall into first catch block and that catch will also trying to rollback and that will also result in error. Since the second catch will also test for sqlException so effectivily that will throw an Exception not handled error. Can you this exception handling is wrong?
try
{
adapter.Update(dataSet.Tab
tran.Commit();
DisplayTextBox.AppendText(
}
catch (SqlException ex)
{
try
{
// Package up and display the errors
foreach (SqlError errSQL in ex.Errors)
{
writeString.WriteLine("Err
writeString.WriteLine(" Message: {0}", errSQL.Message);
writeString.WriteLine(" Number: {0}", errSQL.Number);
writeString.WriteLine(stri
}
DisplayTextBox.Text = writeString.ToString();
// Roll back the transaction.....
tran.Rollback();
}
catch (SqlException RollbackEx)
{
// Handle exception in rollback in case connection dies
foreach (SqlError err in RollbackEx.Errors)
{
writeString.WriteLine("Err
writeString.WriteLine(" Message: {0}", err.Message);
writeString.WriteLine(" Number: {0}", err.Number);
writeString.WriteLine(stri
}
}
Both instances they are catching SqlException? So say, when something happens just before commiting the transaction that obiviously will fall into first catch block and that catch will also trying to rollback and that will also result in error. Since the second catch will also test for sqlException so effectivily that will throw an Exception not handled error. Can you this exception handling is wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!!!