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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Apr 1, 2011, 2:42 PM EDT
FroglegRank #1,020 · 1.9K reputationPosted Apr 1, 2011, 3:23 PM EDT
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
Sam HobbsRank #107 · 20.5K reputationPosted Apr 1, 2011, 6:51 PM EDT
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.
Mahesh ChandRank #1 · 393.3K reputationPosted Apr 1, 2011, 7:12 PM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 2, 2011, 4:19 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Apr 2, 2011, 5:19 AM EDT
Let me emphasize this. It is frustrating for me when people post code of their own with catch blocks such as that.
VulpesRank #55 · 29.2K reputationPosted Apr 2, 2011, 9:57 AM EDT
theLizardRank #1,079 · 1.8K reputationPosted Apr 2, 2011, 2:28 PM EDT
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.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 2, 2011, 3:14 PM EDT