Calling WebService with using and try catch

H

homawong

Hi,

This seems to be an obvious question but I ask anyway.

Currently, I call webservice as follow,

try
{
using (webService.someService ws = new .webService.someService())
{
// Do something
}
}
catch (System.Net.WebException ex)
{
// Catch exception
}

Sometimes I want to get some information about the webservice within
the catch block, so this structure prevent me from doing it and I have
to add another using(...) block within the catch code.

One simple way to get around it is swapping the try/catch and using,

using (webService.someService ws = new .webService.someService())
{
try
{
// Do something
}
catch (System.Net.WebException ex)
{
// Catch exception
}
}

my question is when an unhandled exception is thrown, the using(...)
block should "theorically" effective and clean up the webservice
correctly. But would there be things that might happen causing the
clean up of webservice fail? If this is the case, then with using
outside of try/catch might not be a good idea.

But if there is such things, I think the original one will also fail.
Any idea on this?

Homa Wong
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top