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
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();
}
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
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
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);
}
}
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