Using or TryCatchFinally

K

K Viltersten

I can't decide whether i should deploy the
using statement or Try-Catch-Finally
statement.

I'd prefer using because it's more compact
and automagically disposes stuff.

On the other hand, i can't be sure that the
DB is working, so i might end up with an
ugly exception, which requires an explicit
try/catch to handle...

Suggestions?
 
M

Marvin Landman

K said:
I can't decide whether i should deploy the
using statement or Try-Catch-Finally statement.

I'd prefer using because it's more compact
and automagically disposes stuff.

On the other hand, i can't be sure that the DB is working, so i might
end up with an ugly exception, which requires an explicit try/catch to
handle...

Suggestions?

IDisposable resource = ...;
using (resource) {
...
}

is functionally equivalent to

IDisposable resource = ...;
try {
...
} finally {
if (resource != null)
resource.Dispose ();
}

(The compiler creates a temp variable to ensure that resource is not
changing.)

As you can see there is no catch block in the using statement
implementation.

So if you need a catch block then you have to create a catch block even
if you choose the using statement over a try-finally block.

Marvin
 
K

K Viltersten

So if you need a catch block then you have to create
a catch block even if you choose the using statement
over a try-finally block.

Dang... :)

Thanks to both of you.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top