tags:

views:

113

answers:

5

hi i have a question i have some code that works good on like 70% of the computers i use it on but.. for some reason theres a few that are pesky and id like to do something like this (keep in mind this is a hypothetical)

private void test_click(object sender, EventArgs e)
{
  MessageBox.Show("hi");
  //if it works ok without a error it continues to
  MessageBox.Show("worked ok");
  //if it encountered a error of some kind it would go to
  MessageBox.Show("DID NOT WORK OK");
}
+1  A: 

I would suggest log information rather then pops up messages.

Arseny
+4  A: 

What about try-catch (or try-catch-finally)?

private void test_click(object sender, EventArgs e)
{
  MessageBox.Show("hi");
  try
  {
     //if it works ok without a error it continues to
     MessageBox.Show("worked ok");
  }
  catch( Exception )
  {  
    //if it encountered a error of some kind it would go to
    MessageBox.Show("DID NOT WORK OK");
  }
}

Attention: Here I use a global catch( Exception ) which should be used with care, only! For a test method this is no problem, but do not do this in production code. You should at least specify the expected exception there and think about how to handle the situation.

tanascius
+1. I posted almost exactly the same answer as you did (but I was slower. :) **Note:** You can simplify `catch (Exception)` to just `catch`. That said, I agree that in most cases, it's not a good idea. The code should only catch exceptions that it knows how to handle; all other exceptions (ie. the majority) should not be caught, or at least they should be re-thrown using `throw;`.
stakx
+1 for providing a useful answer to a bad question!
Andras Zoltan
worked like a PRO
NightsEVil
A: 

you can use try catch.

jolly
A: 

When you say it doesn't work, do you mean it throws an exception, or that it just silently fails without any explanation?

If it throws an exception, you should use something like

private void test_click(object sender, EventArgs e)
{
    try
    {
        MessageBox.Show("hi");
        MessageBox.Show("worked ok");
    }
    catch(WheteverExceptionType ex)
    {
        MessageBox.Show("DID NOT WORK OK");
        // you can also access the properties of the thrown exception "ex" here...
        MessageBox.Show(ex.Message);
    }
}
ZombieSheep
I understand what you're trying to show with this bit of code (as it is at the time of this writing), but note that `MessageBox.Show()` will most likely *never* throw any exception. Thus, in this case, a `catch` block is wasted time. You'd need to do something else besides showing message boxes.
stakx
absolutely agree. From the code posted by the OP, I assumed he was trying to do something non-trivial, but rather than post the details he'd posted MessageBox.Show() code in its place. Apologies to the OP if that assumption was wring. :)
ZombieSheep
A: 

Essentially:

try {
  MessageBox.Show("hi");
  DoSomethingThatMightFail();
  MessageBox.Show("worked ok");
} catch (DoSomethingFailedException e) {
  MessageBox.Show("Something did not work: " + e.Message);
}
Richard