Need help on ADO.Net

J

John

Hi,

I use try, catch and finallay to control Sql connection, and run into 1
problem. Some of the sql errors, I don't want the program flow to go to
catch block.

Here is my code snippet:

SqlConnection objSqlConn;

objSqlConn = new SqlConnection(strConnectionString);

try

{

objSqlConn.Open();

//Some SQL statements, if errors go to catch block


//Other SQL statements, if errors DO NOT go to catch block

}

catch(SqlException e)

{

//Do error handling here

}

finally

{

objSqlConn.Close();

}

How do I make it happen?

TIA
 
G

Guest

John,
We need to make the distinction between Business Logic and Exceptions here.

if program flow does go into your Catch block, that's because you have an
exception - which is NOT a normal, expected part of the business logic. The
best way to avoid this is to test for whatever business conditions might
cause an exception first, and handle them appropriately BEFORE program flow
ends up in a catch block with an exception.

Failing that , you COULD call a separate method from inside your catch block
after whatever "Tests" you need to do dictate that you should do so. And,
that method might very well make additional SQL calls.

Peter
 
G

Guest

I agree with Peter - exceptions should be exactly what they're called - exceptions (and not valid business logic).

That aside, there is a simply way you can accomplish what you want: move the 'don't catch my errors' logic out of the try/catch block.

Chris Trelawny-Ross
Hi,

I use try, catch and finallay to control Sql connection, and run into 1
problem. Some of the sql errors, I don't want the program flow to go to
catch block.

Here is my code snippet:

SqlConnection objSqlConn;

objSqlConn = new SqlConnection(strConnectionString);

try

{

objSqlConn.Open();

//Some SQL statements, if errors go to catch block


//Other SQL statements, if errors DO NOT go to catch block

}

catch(SqlException e)

{

//Do error handling here

}

finally

{

objSqlConn.Close();

}

How do I make it happen?

TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top