In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following:
try
{ //do something}
catch (ex){}
In the visual studio ide, I see a a curly green under the ex.
I also do not see any using statement that I would think applies.
Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me?
I put this kind of code around some of my new code and everything falls into the catch block.
Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the
using statement?
Loading
Sam HobbsPosted Dec 10, 2011, 5:52 AM
Note that if you say something like "MessageBox.Show("Open error: " + ex.ToString());" then you will get the entire error; all the information, which might be too much for non-technical users but it can help you when you are debugging and such.
Usman ZafarPosted Dec 9, 2011, 1:33 PM
try
{
File.Delete(path);
}
catch(Exception ex)
{
MessageBox.Show(ex.Messsage);
}
But sometimes it can be like this:
try
{
File.Delete(path);
}
catch( FileNotFoundException ex)
{
MessageBox.Show("File does not exist");
}
catch( Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Coming out of try catch block");
}
Pravin MorePosted Nov 2, 2011, 12:13 AM
Make sure you have declared variable 'ex' like below..............
for e.g
try
{ //do something}
catch (Exception ex){}
n Exception class belongs to System namespace.
i.e using System
Thanks,
pravin
Mark it as "Accepted Answer" if its helped you.