using and error trapping

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I've just started using the 'using' keyword for database connections
etc. Is it not possible to use try catch blocks with using? And if so,
how do you trap errors when using using?
 
Mike said:
I've just started using the 'using' keyword for database connections
etc. Is it not possible to use try catch blocks with using? And if so,
how do you trap errors when using using?

It is possible to do so.

If you want to continue with using, try this:

try
{
using (SomeObject obj = new SomeObject())
{
... something that can go wrong here
}
}
catch (YourExceptionClassHere ex)
{
}

Or you can nest the try/catch block inside the using block.
 
I've just started using the 'using' keyword for database connections
etc.  Is it not possible to use try catch blocks with using? And if so,
how do you trap errors when using using?

*** Sent via Developersdexhttp://www.developersdex.com***

Hi,

in exactly the same way:
try{
using() {}
}catch{}

or
using(){
try{
}catch{}
}

In the first case the connection will be closed (and out of context)
in the catch block

In the second the connection might still be open and in context so you
can access it
 

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

Back
Top