Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try-Catch and "Continue" - is this possible?

Tags:

c#

.net

try-catch

I have a section in my code where I am querying all SQL Server Databases on my network. I am first trying to use a SQL Login to access the SQL Server Instance but if that fails then I want to try connecting using my Windows Credentials. After that if I still can't connect then I want the code to fail and then notify the user.

So I guess what I am asking is how can I loop back from inside of a Try-Catch block to the line just above the Try-Catch block:

String conxString = @"Data Source=SQLInstance1;User ID=FOO;Password=BAR;";
bool secondTime = false;

using (SqlConnection sqlConx = new SqlConnection(conxString))
     {
         Try{
               sqlConx.Open();
               DataTable tblDatabases = sqlConx.GetSchema("Databases");
               sqlConx.Close();
               secondTime = false;
               Console.WriteLine("SQL Server found!");
         }
         Catch(System.Data.SqlClient.SqlException e){
                if (!secondTime){
                   secondTime = true;
                   conxString = @"Data Source=SQLInstance1; Integrated Security=True;";
                      //Loop back to the using statement to try again with Windows Creds
                {
                 else{
                   Console.WriteLine("SQL Server not found or credentials refused");
                 }
                   //Report Failure to connect to user

         }
         finally{
            //Reset Variable
            secondTime = false;
         }

      }
like image 593
Mark Kram Avatar asked May 26 '11 23:05

Mark Kram


People also ask

Is try and catch is possible?

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System. exit() it will execute always.

Does program continue after try catch?

If an exception is thrown inside the catch-block and that exception is not caught, the catch-block is interrupted just like the try-block would have been. When the catch block is finished the program continues with any statements following the catch block.

Is it possible try block and catch section?

As I mentioned above, a single try block can have any number of catch blocks. 2. A generic catch block can handle all the exceptions. Whether it is ArrayIndexOutOfBoundsException or ArithmeticException or NullPointerException or any other type of exception, this handles all of them.

Does try catch catch all errors?

So, try... catch can only handle errors that occur in valid code. Such errors are called “runtime errors” or, sometimes, “exceptions”.


1 Answers

I would probably go this route:

String conxString = @"Data Source=Instance1;User ID=FOO;Password=BAR;";
//in your main function
if(!TryConnect(conxString))
{
   Console.WriteLine("SQL Creditials failed.  Trying with windows credentials...");
   conxString = "new conn string";
   TryConnect(conxString);
}
..............
//new function outside of your main function
private bool TryConnect(string connString)
{
   using (SqlConnection sqlConx = new SqlConnection(conxString))
     {
         Try{
               sqlConx.Open();
               DataTable tblDatabases = sqlConx.GetSchema("Databases");
               sqlConx.Close();
         }
         Catch(System.Data.SqlClient.SqlException e){
                return false;
         }
         return true;    
      }
}
like image 191
Abe Miessler Avatar answered Oct 01 '22 17:10

Abe Miessler