Will the connection close?
-
Hi! Simple C# question, I'm just wondering the the connection will close in the code below:
try {
connection.Open();
command.ExecuteNonQuery();
throw new Exception();
} catch(Exception) {
throw;
}
finally {
connection.Close();
}Rafferty
-
Hi! Simple C# question, I'm just wondering the the connection will close in the code below:
try {
connection.Open();
command.ExecuteNonQuery();
throw new Exception();
} catch(Exception) {
throw;
}
finally {
connection.Close();
}Rafferty
-
Hi! Simple C# question, I'm just wondering the the connection will close in the code below:
try {
connection.Open();
command.ExecuteNonQuery();
throw new Exception();
} catch(Exception) {
throw;
}
finally {
connection.Close();
}Rafferty
Yes
Navaneeth How to use google | Ask smart questions
-
yes becouse finally block gets excecuted in any situation whether there is exception or code excecutes successfully.
rahul
So, in the statement sequence, will the
connection.Close()
be executed before or after the exception is rethrown in thecatch(...)
block? If after, what if this whole thing is contained in anothertry... catch
statement, will the catch statement in the parent next be executed first?Rafferty
-
So, in the statement sequence, will the
connection.Close()
be executed before or after the exception is rethrown in thecatch(...)
block? If after, what if this whole thing is contained in anothertry... catch
statement, will the catch statement in the parent next be executed first?Rafferty
What happened when you tried? All you are asking can be easily checked by writing a simple program.
Navaneeth How to use google | Ask smart questions
-
What happened when you tried? All you are asking can be easily checked by writing a simple program.
Navaneeth How to use google | Ask smart questions
haha okay okay, i'll try then.
Rafferty
-
So, in the statement sequence, will the
connection.Close()
be executed before or after the exception is rethrown in thecatch(...)
block? If after, what if this whole thing is contained in anothertry... catch
statement, will the catch statement in the parent next be executed first?Rafferty
-
it will excecuted after the exception is rethrown and the outer catch block will be excecuted next.
rahul
You're right! I just made a small experiment as Navaneeth suggested and it looks like the case. Thanks much!
Rafferty