TL ; DR:
Syntax errors are mistakes in the use of the Scala language, analogous to spelling or grammar mistakes in a language like English. For example:
Omitting quotes when working with strings:
println("Welcome!)Omitting parentheses when using the
printlnfunction:println("Let's code!"When Scala encounters a syntax error, it reports a compile error and your program does not run at all.
Full lesson:
Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.
Here are two common errors that we encounter while writing Scala code:
The syntax error:
Syntax errors are mistakes in the use of the Scala language, and are analogous to spelling or grammar mistakes in a language like English. For example, the sentence "Would you some tea?" does not make sense – it is missing a verb.
Syntax error means there is something wrong with the way your program is written. Some examples:
1. Mismatched quotes when declaring a string:
var greeting = 'Hey there!"
When we run this code we'll get an error such as unclosed string literal.
2. Leaving out a parenthesis:
println"Hey")
When we run this code we'll get a syntax error, because the closing parenthesis has no opening one.
The reference error:
A reference error occurs when the Scala compiler sees a word it does not recognize. Some examples:
1. Misspelling a class / function name:
prntln("Hey")
When we run this code we'll get the error Not found: prntln.
2. Using a non-existent variable / Misspelling a variable's name:
var animal = "parrot"
println(aniimal)
When we run this code we'll get the error Not found: aniimal.
Assignment
Follow the Coding Tutorial and let's practice with errors!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. Premium is unlimited.
