-2

I have a really simple question (I'm new to c# visual studios) about the try catch method:

try

{
double seven = 7
MessageBox.Show("You Picked 7!");
}

catch(Exception ex)

{
MessageBox.Show(ex.Message);
}

I tried replacing the MessageBox.Show(ex.Message); with MessageBox.Show("Please enter a number."); but it won't work.

How should I approach this? There is the error line under the ("Please enter a number."). Thanks in advance!

D. Dude
  • 11
  • 1
  • 4
  • squigglies are great, but whats the build error? – scniro Oct 07 '15 at 02:57
  • 1
    Doesn't matter if you are new to C# or anything - but you are acting as if you are new to asking questions. That "red squiggly line" indicates an error. How do you expect anyone to help if you do not include the error in your question? – JK. Oct 07 '15 at 02:59
  • How do you look at the build error? Once again, I'm sorry, I'm really new to programming. – D. Dude Oct 07 '15 at 03:00
  • just build your solution and observe the output window. The error should be fairly obvious if one is encountered – scniro Oct 07 '15 at 03:00
  • weird code, what exception will be thrown by a simple assignment to 7 and then showing a messagebox ? None. – Philip Stuyck Oct 07 '15 at 05:57
  • He maybe currently at an early stage of a project and needs help understanding some fundamentals, and as he stated is very new to C# _AND_ Visual Studio. Try being more helpful next time... – David Carrigan Oct 07 '15 at 15:30

1 Answers1

-2

You're missing a semi-colon after declaring 'seven'

Below sample works for me.

try
{
    double seven = 7;
    MessageBox.Show("You Picked 7!");
}
catch(Exception ex)
{
    MessageBox.Show("Please enter a number.");
}

Just a tip to if you plan on not using the exception then it's recommended you don't specify an exception as a parameter to catch. See Using catch without arguments

try
{
    double seven = 7;
    MessageBox.Show("You Picked 7!");
}
catch
{
    MessageBox.Show("Please enter a number.");
}
Community
  • 1
  • 1
David Carrigan
  • 751
  • 1
  • 8
  • 21
  • Thanks! It was an accident. All I had to do was clean the solution. – D. Dude Oct 07 '15 at 03:23
  • @D.Dude glad you got it to work! When in doubt, clean and rebuild Lol – David Carrigan Oct 07 '15 at 03:24
  • strange that this is downvoted so much, the semicolon was missing. The code is terrible but so is the op's code – Philip Stuyck Oct 07 '15 at 05:58
  • Yeah I don't get it either @PhilipStuyck I personally don't like the culture here at Stack overflow sometimes. But the internet is the internet and where ever you find it, you'll find trolls and people with bad attitudes whom can't contain themselves in the light of helping others. – David Carrigan Oct 07 '15 at 12:17