Exception in finally black ???
-
Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance
-
Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance
-
Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance
You can write a piece of code to see what happends :)
Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
-
Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance
In your case check if the connection state is open before closing it. :)
Arun Jacob http://codepronet.blogspot.com/
-
Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance
Specifically for your example, you could use a
try
/catch
insidefinally
, thus:try {
// do something
// with con
}
catch (Exception ex) {
// Ya boo sucks!
}
finally {
try {
con.close();
}
catch (Exception ex) {
// unable to close it but must let go!
}
finally {
// finally, finally release the reference
con = null;
}
}Easy (and with tags too)
Panic, Chaos, Destruction. My work here is done.
-
Specifically for your example, you could use a
try
/catch
insidefinally
, thus:try {
// do something
// with con
}
catch (Exception ex) {
// Ya boo sucks!
}
finally {
try {
con.close();
}
catch (Exception ex) {
// unable to close it but must let go!
}
finally {
// finally, finally release the reference
con = null;
}
}Easy (and with tags too)
Panic, Chaos, Destruction. My work here is done.
I'd recommend using a guard clause instead. I know you've answered the OP here, but it's better to do the following:
try
{
}
catch (...)
{
}
finally
{
if (conn.Open)
conn.Close();
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
I'd recommend using a guard clause instead. I know you've answered the OP here, but it's better to do the following:
try
{
}
catch (...)
{
}
finally
{
if (conn.Open)
conn.Close();
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Probably better [he says after checking that is exactly what I have done] but I do have the closure in the finally block inside it's own try/catch.
Panic, Chaos, Destruction. My work here is done.
-
Specifically for your example, you could use a
try
/catch
insidefinally
, thus:try {
// do something
// with con
}
catch (Exception ex) {
// Ya boo sucks!
}
finally {
try {
con.close();
}
catch (Exception ex) {
// unable to close it but must let go!
}
finally {
// finally, finally release the reference
con = null;
}
}Easy (and with tags too)
Panic, Chaos, Destruction. My work here is done.
Wouldn't
using (whateverConnection conn = new whateverConnection()) { try { // do stuff } catch { // complain a lot } }
be a better approach for the OP?
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance