473,499 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using And Try Catch in harmony

Whats the best way of handing exception of using?
Is it method 1 or method 2 or some other way
########## Method 1 ###########
using(something)
{
try
{
}
catch
{
}

}

########## Method 2 ###########
using(something)
{
try
{
}
catch
{
}
finally
{
}

}
Mar 7 '08 #1
3 1709
On Fri, 07 Mar 2008 11:19:34 -0800, parez <ps*****@gmail.comwrote:
Whats the best way of handing exception of using?
There's no practical difference between the two options that you posted,
other than the presence of the "finally" block. You'd use "finally" for
the same reasons you'd use it otherwise: you have something that always
has to be done before exiting the try/catch/finally section, whether an
exception occurs or not.

Obviously, if the only reason you would have had a "finally" block before
is to dispose the "something" you've put in the "using" statement, then
you wouldn't need "finally". But otherwise, nothing has changed.

And next time, please indent any code you post. Thanks.

Pete
Mar 7 '08 #2
I think you rather should put your using clause within the try catch. It
would make more sense, right?

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 07 Mar 2008 11:19:34 -0800, parez <ps*****@gmail.comwrote:
>Whats the best way of handing exception of using?

There's no practical difference between the two options that you posted,
other than the presence of the "finally" block. You'd use "finally" for
the same reasons you'd use it otherwise: you have something that always
has to be done before exiting the try/catch/finally section, whether an
exception occurs or not.

Obviously, if the only reason you would have had a "finally" block before
is to dispose the "something" you've put in the "using" statement, then
you wouldn't need "finally". But otherwise, nothing has changed.

And next time, please indent any code you post. Thanks.

Pete

Mar 7 '08 #3
On Fri, 07 Mar 2008 11:38:56 -0800, Robson Felix <ro****@robsonfelix.com>
wrote:
I think you rather should put your using clause within the try catch. It
would make more sense, right?
It might. It might not. It really depends on what you're trying to do.
Without seeing of the rest of the code, it's impossible to say, as there's
no general-purpose rule that applies in all situations.

Pete
Mar 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
21361
by: Junior | last post by:
I keep receiving this "The type or namespace name 'CASsEventHandler' could not be found (are you missing a using directive or an assembly reference?)" message in two particular lines, and I've...
4
4330
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
6
1708
by: cody | last post by:
Mostly always you use the using clause you deal with native ressources, so exception handlinjg is sooner or later inevitable. so why do we need to write try { using (File f =...
1
2296
by: gemel | last post by:
I wish to implement error recovery using the Exception mechanism However, I'm not sure I fully appreciate some of the detail in which this works. The code that contains a potential error is...
10
2474
by: George G. | last post by:
Hi there, I am busy writing a new asp.net application and I am reusing some of my existing asp functions and methods in a user control. I need access to session, request and response in some of...
6
17133
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
21
39433
by: James Black | last post by:
I am curious if there is a benefit to set attributes directly, in my javascript, or to use setAttribute. For example, I have this: var input = document.createElementNS(xhtmlNS, 'input');...
1
2365
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
24
2344
by: Chameleon | last post by:
Is there a possibility to create memory leak, the code below if I run the line: --------------------------------------------------------- MyClass cl = new MyClass();...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.