|
-
Jun 24th, 2007, 08:53 PM
#1
Thread Starter
Addicted Member
Difference with using {} and try{} Catch{}
Hi Everyone,
Just a quick question, what is the difference between the using statement and the Try, Catch block?
i know that i have to manually dispose of the connection and command objects in the finally statement, but i always put something in the catch block, for error handling.
how is this error handling used in the using block?
Cheers,
Justin
-
Jun 24th, 2007, 09:48 PM
#2
Re: Difference with using {} and try{} Catch{}
The 'using' statement negates the need for a 'finally' block by guaranteeing that the object created will be disposed no matter what occurs inside the 'using' block itself. The 'using' block provides no error handling mechanism, but the object created by the using statement will always be disposed when the block execution exits the block, no matter how that happens. That includes an exception being thrown and not caught. In short, this:
C# Code:
using (IDosposable obj = new IDisposable())
{
try
{
}
catch
{
}
}
is equivalent to this:
C# Code:
IDosposable obj = new IDisposable();
try
{
}
catch
{
}
finally
{
obj.Dispose();
}
-
Jun 25th, 2007, 12:11 AM
#3
Thread Starter
Addicted Member
Re: Difference with using {} and try{} Catch{}
Hi jmcilhinney,
Thanks for the info, i was aware of both, but was not aware you could use both together, but it makes sense.
Cheers, and thanks for all your help.
Justin
-
Jun 27th, 2007, 11:01 PM
#4
Thread Starter
Addicted Member
Re: Difference with using {} and try{} Catch{}
Hi Again,
Say if i had to work with 2 or 3 different streamreaders/writers in the same void, could i possibly do this:
Code:
public TransLog() {
try {
string _date = DateTime.Now.ToShortDateString();
string _fileName = Environment.CurrentDirectory + "\\logs\\translog_" + _date;
if (File.Exists(_fileName) == true) {
using (FileStream _fileStream = new FileStream(_fileName, FileAccess.ReadWrite)) {
}
} else {
}
} catch (Exception ex) {
}
}
and the catch statement would still catch any exceptions that could occur within the using the statement...
or would i need to do something like this, and add try{} catch{} to each of my using{} statements:
Code:
public TransLog() {
try {
string _date = DateTime.Now.ToShortDateString();
string _fileName = Environment.CurrentDirectory + "\\logs\\translog_" + _date;
if (File.Exists(_fileName) == true) {
using (FileStream _fileStream = new FileStream(_fileName, FileAccess.ReadWrite)) {
try {
} catch (Exception ex) {
}
}
} else {
}
} catch (Exception ex) {
}
}
and the top level try{} catch{} is there to catch any errors outside the using block...
Thanks, Justin
-
Jun 28th, 2007, 10:32 AM
#5
Re: Difference with using {} and try{} Catch{}
Give it a shot.
Your first one will work.
c# Code:
// Catch exceptions
private void button1_Click(object sender, EventArgs e)
{
try
{
using // Whatever
{
int x = 0;
int y = 100;
int t = y / x;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Last edited by nmadd; Jun 28th, 2007 at 10:36 AM.
Reason: added c# tags
-
Jun 28th, 2007, 11:32 AM
#6
Re: Difference with using {} and try{} Catch{}
I wouldn't say that Using negates the use of Finally.... it does if all the finally does is .Dispose the object.
Rather the Using negates the need to explicitly create the object and dispose of it at the end.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|