• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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?
0
ube100
Asked:
ube100
  • 2
  • 2
1 Solution
 
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
 
ube100Author Commented:
Thanks!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now