1

Possible Duplicate:
How to catch exceptions

I have not really had to use try and catch exceptions. I am trying to use a try/catch to trap for potential errors. Now I am not to sure where to put the try and catch this is the code I have right now..

 divide d;
    private void button1_Click(object sender, EventArgs e)
    {
        d = new divide(int.Parse(textBox1.Text), int.Parse(textBox2.Text));
        int total = d.CalculateDivision();
        MessageBox.Show(total.ToString());
    }

now would I put it under here with

try
{

}
catch
{
MessageBox.Show("error");
}

or would I add the try/ catch somewhere in the code.

Community
  • 1
  • 1
shan
  • 1,311
  • 4
  • 13
  • 17
  • Where is the question? what is `new divide`? – gdoron Mar 22 '12 at 09:57
  • 1
    You know try/catch is bit costly. So you can use TryParse APIs and do a bit of refactoring to above code to avoid exceptions. – Zenwalker Mar 22 '12 at 09:57
  • If its web application you can use Application_Error event in Global.asax .. see http://stackoverflow.com/questions/9806832/general-exception-handling-without-global-asax-file – Flowerking Mar 22 '12 at 09:57
  • Google it.. or check out MSDN http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx – Kaf Mar 22 '12 at 09:58
  • 1
    You shouldn't "trap for POTENTIAL errors". Fail fast, do not try to continue a possibly flawed process. – Guillaume Mar 22 '12 at 09:58
  • 2
    I honestly hate it when you downvote newbies without explanation: @shan: you got the downvotes because there are already questions on this - so please read the FAQ and use the site-search before asking questions. – Random Dev Mar 22 '12 at 09:59
  • 1
    If you want to learn about new things, it's best to start testing in a smaller program that doesn't involve user interactions or events, so that you can see what the program flow is without relying on things the system does. – Mr Lister Mar 22 '12 at 09:59

4 Answers4

2

see http://msdn.microsoft.com/en-us/library/ms173160.aspx

The try goes around the code where the exception is thrown and the value is handled. in your example:

 divide d;
private void button1_Click(object sender, EventArgs e)
{
  try
  {

    d = new divide(int.Parse(textBox1.Text), int.Parse(textBox2.Text));
    int total = d.CalculateDivision();
    MessageBox.Show(total.ToString());
  }
  catch(Exception)
  {
     MessageBox.Show("error");
  }
}

as you can only show the total if there is no exception.

plaugg
  • 420
  • 3
  • 11
1

Nope, that is right ;). Just use it like what you showed us there:

try
{
    // Your Code.
}
catch (Exception ex)
{
    MessageBox.Show(ex);
}
Yuki Kutsuya
  • 3,968
  • 11
  • 46
  • 63
1

You pretty much have the answer, if an exception is thrown though you can do this to get some more information about what might have caused it:

try
{
    //your code:
    d = new divide(int.Parse(textBox1.Text), int.Parse(textBox2.Text));
    int total = d.CalculateDivision();
    MessageBox.Show(total.ToString());
}
catch (Exception ex)
{
    MessageBox.Show("Error has occured! " + ex.Message);
}

Another tip for you seeing as you are learning about Exception handling is to take a look at the finally block, this will get executed whether or not there was an exception, it goes after the try and catch blocks:

finally
{
    // this code will always execute, maybe do some cleanup here
}
DukeOfMarmalade
  • 2,680
  • 10
  • 41
  • 70
0

You would do something like this:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
       d = new divide(int.Parse(textBox1.Text), int.Parse(textBox2.Text));
       int total = d.CalculateDivision();
       MessageBox.Show(total.ToString());
    }
    catch(Exception error)
    {
        MessageBox.Show(error.Message);
    }
}
Random Dev
  • 51,810
  • 9
  • 92
  • 119