Link to home
Start Free TrialLog in
Avatar of ube100
ube100Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Nested exception handle

Hi, Have a look at this event handler:

 private void TransactionButton_Click(object sender, EventArgs e)
        {
            ClearDisplay();

            StringWriter writeString = new StringWriter();
            SqlTransaction tran = null;
            SqlDataAdapter adapter = null;

            using (SqlConnection connection = new System.Data.SqlClient.SqlConnection(
                Errors.Properties.Settings.Default.NorthwindConnectionString))
            {
                try
                {
                    adapter = new SqlDataAdapter();
                    // Reset the adapert's update commands using the new instance of the connection.
                    SetCommands(adapter, connection);
                    connection.Open();
                    tran = connection.BeginTransaction(IsolationLevel.ReadCommitted);

                    //Set the commands to use the transaction
                    adapter.UpdateCommand.Transaction = tran;
                    adapter.InsertCommand.Transaction = tran;
                    adapter.DeleteCommand.Transaction = tran;
                }
                catch (Exception ex)
                {
                    DisplayTextBox.Text = ex.Message;
                    // Early exit on error
                    return;
                }

                try
                {
                    adapter.Update(dataSet.Tables["Customers"]);
                    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("Error:");
                            writeString.WriteLine(" Message: {0}", errSQL.Message);
                            writeString.WriteLine(" Number: {0}", errSQL.Number);
                            writeString.WriteLine(string.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("Error in Rollback:");
                            writeString.WriteLine(" Message: {0}", err.Message);
                            writeString.WriteLine(" Number: {0}", err.Number);
                            writeString.WriteLine(string.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?
Avatar of multithreading
multithreading

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.)
Avatar of ube100

ASKER

This is what they doing:

try
                {
                    adapter.Update(dataSet.Tables["Customers"]);
                    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("Error:");
                            writeString.WriteLine(" Message: {0}", errSQL.Message);
                            writeString.WriteLine(" Number: {0}", errSQL.Number);
                            writeString.WriteLine(string.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("Error in Rollback:");
                            writeString.WriteLine(" Message: {0}", err.Message);
                            writeString.WriteLine(" Number: {0}", err.Number);
                            writeString.WriteLine(string.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?
ASKER CERTIFIED SOLUTION
Avatar of multithreading
multithreading

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ube100

ASKER

Thanks!!!