Using Statement & Catch

  • Thread starter Thread starter Fred Chateau
  • Start date Start date
F

Fred Chateau

To catch an exception associated with resources within a Using block, does
the Catch block go within the scope of the Using block or following it?

If within the scope of the Using block, what about nested Using statements?
Will a Catch block in the innermost Using block catch all exceptions?
 
To catch an exception associated with resources within a Using block, does
the Catch block go within the scope of the Using block or following it?

Depends on what you want. If you put it inside the using block, it
will not catch exceptions thrown by the Dispose method. If you put a
try/catch around the using block, the resource variable will be out of
scope and not usable from the catch block.


Mattias
 
Fred Chateau said:
To catch an exception associated with resources within a Using block, does
the Catch block go within the scope of the Using block or following it?

Never mind...

I don't think you can use a Catch statement without a corresponding Try
statement, or at least Intellisense wouldn't accept it without one.

Regards,

Fred Chateau
 
Mattias,
Depends on what you want. If you put it inside the using block, it
will not catch exceptions thrown by the Dispose method. If you put a
try/catch around the using block, the resource variable will be out of
scope and not usable from the catch block.

Very secure answered.

:-)

Cor
 

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