catch(Exception ex) {}
The reason I ask is that there are some articles in this web site that do that. Is it good to show that as samples of good programming?
catch(Exception ex) {}
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Apr 2, 2011, 7:14 PM
theLizardPosted Apr 2, 2011, 6:28 PM
Frogleg's example is not a really good one because I would never see myself doing that in a try catch block, no need to, I would simply do this
using (StreamReader sr = new StreamReader(@"text.txt"))
{
string line;
while ((line = sr.ReadLine()) != null && line.IndexOf(",") > 0)
{
string[] str = line.Split(',');
// do whatever
}
}
I do things like
bool success = false;
try
{
if(blah blah == getFromDatabase("harry potter"))
success = true;
}
catch(Exceptionex)
{
//db error may have occurred don't care
}
return(success)
in this case if there is a database error which I am really not interested in, the catch stops an unconditional crash, and the return value allows me to handle what I need to do.
Bottom line is not all try, catches need to handle exceptions with messages to the user.
VulpesPosted Apr 2, 2011, 1:57 PM
Sam HobbsPosted Apr 2, 2011, 9:19 AM
Let me emphasize this. It is frustrating for me when people post code of their own with catch blocks such as that.
VulpesPosted Apr 2, 2011, 8:19 AM
Mahesh ChandPosted Apr 1, 2011, 11:12 PM
Sam HobbsPosted Apr 1, 2011, 10:51 PM
I hope that we can get articles to do something more like what Frogleg suggests. I don't want to name a specific article, but there is one I looked at that is failing for some reason. I did not look closely but it is reading a web page as a text file and processing the HTML as text. It is apparently encountering an error and just quits without an error message or anything. So it is likely something such as what Frogleg shows.
I see people posting code in forums with catch blocks as I describe. If people have problems with code samples from articles in this web site and they post questions here in forums about the code and the problem is hidden by this type of catch block then I think it would be better for sample code to show some kind of an error when relevant. If it is those of us in the forums that have to help with problems such as that then ir is relevant for us to ask that the problem be minimized.
I think I see code such as that more than most members because I edit articles. Fortunately the two of you are so good at English grammar that I can ignore your articles, but I ususally read them anyway because they are so good.
I realize that there is not much we can do about code such as I describe here but I wanted to say something anyway.
FroglegPosted Apr 1, 2011, 7:23 PM
try
{
using (StreamReader sr = new StreamReader("test.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] str = line.Split(',');
// do whatever
}
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
where for some reason there was a space between or especially at the end of the text file - (you don't need the spaces)
or do catch the exception quietly and continue on
VulpesPosted Apr 1, 2011, 6:42 PM