Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Programming languages have rules that allow the compiler to make sense of the code. You need to use the right keywords and punctuation in the right places, or the compiler will get confused and report an error.
Programming languages have rules that allow the compiler to make sense of the code. You need to use the right keywords and punctuation in the right places, or the compiler will get confused and report an error.
Suppose I take our code sample from earlier, and I delete the semicolon from the end of the line that prints "Hello World!"
...
class Program
{
static void Main(string[] args)
{
// Displays "Hello World!" on the terminal.
System.Console.WriteLine("Hello World!")
}
}
- You'll be told there are errors in the program that need to be fixed.
- The first line of the output has info about the error:
Program.cs(6,49): error CS1002: ; expected [/home/treehouse/workspace/workspace.csproj]
- The first item on this line is
Program.cs
, the name of the file where the error occurred. - The second item,
(6, 49)
, is the line and column number within the code where the error was detected.- You can see line numbers here on the left side of my editor. Let me move to line 6...
- ...And then move to the end of the line.
- You can see the displayed line and column position updating as I move my cursor.
- You can see that the end of line 6 is at column 49, which is where the error was reported.
- The third item,
error CS1002
, is an error code.- You won't be able to understand this code by itself, but if you ever need more information about an error, try copying this code...
- ...and pasting it into a search engine like Google.
- The first link in the results leads to Microsoft documentation. Let's click that...
- It says "The compiler detected a missing semicolon. A semicolon in required at the end of every statement in C#."
- So it looks like the semicolon character was required, and we introduced an error when we deleted it.
- The fourth item in the output is a brief description of the error:
; expected
- So we probably could have avoided searching Google for the error code in this case. The error description makes it clear that we just need a semicolon.
- Although even for an error this simple, the documentation included additional information that wasn't in the error description.
- Let me re-add the missing semicolon...
- And save my changes...
- And click back in the terminal area.
- I want to use the same command as before,
dotnet run
... - So I'll just hit the Up arrow key to bring up my previous command.
- And hit Enter to run it again without any changes.
- Now that the error is fixed, the code compiles and runs!
The error message won't always guide you to the exact place where an error occurs, but it will often get you close.
- Let me try deleting this curly brace character down on Line 8.
- I need to save my changes again.
- This time, instead of selecting "Save" from the "File" menu, I'll use the shortcut key.
- If I click on the File menu, it will show me the shortcut key to use.
- I'm on a Mac, so I need to hold the Command key and press S.
- Windows users would press Control-S instead.
- Now that my changes are saved, let me try running the program again.
- I'll click down in the terminal area...
- And hit the up arrow and press Enter to run the
dotnet run
command again. - I'll get another error:
Program.cs(7,6): error CS1513: } expected [/home/treehouse/workspace/workspace.csproj]
- I get a different error code this time.
- Unsurprisingly, the error message says a curly brace is expected.
- But it reports that the error is on line 7, not line 8.
- We can still fix it by adding the curly brace back on line 8.
- I'll save...
- And run the program again...
- And the error is gone.
If you make a mistake in typing your code, the error won't always be at the exact line and column number the compiler tells you. But it will usually be pretty close to there. To troubleshoot errors:
- Consider the message that accompanies the error. Sometimes it won't be helpful, but other times it will tell you exactly what's wrong.
- Start by looking at the specified line for typos or other obvious mistakes.
- If you don't see anything on the specified line, look at lines above and below there.
- You can also try copying the error code or error message and pasting it into a web search engine.
Error messages may look a little intimidating right now, but after you've fixed a few of them you'll quickly get used to how they work. Just follow the guidelines I've described here and you'll be able to fix many errors on your own.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up