Bad form for using try / catch as a test?

It seems to me that this is so, but I wanted to confirm - this is a bad form to do something like:

try
{
    SqlUpload(table);
}
catch(PrimaryKeyException pke)
{   
          DeleteDuplicatesInTable(table);
          SqlUpload(table);
}
catch(Exception ex)
{
    Console.Write(ex);
}

Is it better to do this in order to potentially save on efficiency if there are no duplicates in the table, or is it better to run a bit of duplicate files anyway? (I accept the conditions under which the table will be loaded if there are no duplicates in the table itself). Also, given how try-catch statements affect performance, would it be even faster to do it this way at all?

I apologize for the rough nature of this example, but this is just to illustrate the point.

+3
source share
7 answers

Yes, this type of code is considered bad in .NET.

You better write code similar to

if(HasPrimaryKeyViolations(table))
    DeletePrimaryKeyViolations(table)

SqlUpload(table)
+1

, . , Linq2Sql DataContext SubmitChanges(). . (. ).

, - , , . , "" ( , ). , , , .

DataContext.SubmitChanges

.

. , Microsoft , . , .

      DbTransaction transaction = null;
        try
        {
            try
            {
                if (this.provider.Connection.State == ConnectionState.Open)
                {
                    this.provider.ClearConnection();
                }
                if (this.provider.Connection.State == ConnectionState.Closed)
                {
                    this.provider.Connection.Open();
                    flag = true;
                }
                transaction = this.provider.Connection.BeginTransaction(IsolationLevel.ReadCommitted);
                this.provider.Transaction = transaction;
                new ChangeProcessor(this.services, this).SubmitChanges(failureMode);
                this.AcceptChanges();
                this.provider.ClearConnection();
                transaction.Commit();
            }
            catch
            {
                if (transaction != null)
                {
                    try
                    {
                        transaction.Rollback();
                    }
                    catch
                    {
                    }
                }
                throw;
            }
            return;
        }
        finally
        {
            this.provider.Transaction = null;
            if (flag)
            {
                this.provider.Connection.Close();
            }
        }
    }
+3

, , - , , , .

+1

// , - - . try .

, , . try, , , , , - - .

, , , , if. , , , , (, ), try , , , ( ). , .

, ( ). , :

-, , ​​ , , - , . , . , , , . , , , , , , . , - , , .

-, . , int.TryParse int.Parse, . , . brew - , , , , . try/catch , , snide , , :).

, , . "", , ; , . , - , , .

, , , MERGE, table ; , delete-then-insert .

+1

, . .

0

, , SqlUpload() catch, , .

0

, , , try...catch , ( ). , , .

, "" , "" (, , ( ), , ( , ), ). , , (, - (, )).

- , , - , , , .

0

All Articles