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?
ube100Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

multithreadingCommented:
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.)
0
ube100Author Commented:
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?
0
multithreadingCommented:
The question is what kind of exception is being thrown?

Try this: TEMPORARILY change the inner exception to "catch(Exception RoolbackEx)" and see if it catches the exception. If it does look at the type of the RollbackEx. If it isn't derived from SqlException you have found the problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ube100Author Commented:
Thanks!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.