Kinds of Errors
In this lesson, we will look at some of the kinds of errors programmers make.
We'll cover the following
Syntax errors
Just like any natural language, such as English, Java has a set of rules that define it. These rules form the language’s syntax. They dictate how we form phrases and statements. If we do not follow Java’s syntax precisely, we will have made a syntax error, and our program will not compile. The compiler will produce a message that attempts to describe what we have done wrong. Most of the time, these error messages will point right to the mistake, but sometimes the messages might seem vague or even misleading. Because the compiler generates a message when it first detects that something is incorrect, it cannot always diagnose the problem specifically. However, we can be sure of one thing: The compiler is right; we made a mistake!
📝 Note
Compilers detect syntax errors. Any errors detected by a compiler are compile-time errors.
Semantic errors
Even if we follow all of Java’s grammatical rules—its syntax—thereby passing the compiler’s scrutiny, we might not get the results we desire or expect. It is possible for a statement to be syntactically correct but still be wrong. Perhaps a variable has the wrong data type, or we have omitted a pair of parentheses in an expression. Such an error is a semantic error. The semantics of a statement are its meaning. Unlike a natural language, such as English, the semantics of a programming language are unambiguous. A Java statement has one meaning. A statement in English could have several meanings.
Logical errors
Another name for a semantic error is a logical error since it is a mistake in the logic of an algorithm. Although some people differentiate between a logical error and a semantic error, we will consider them to be the same kind of error. These errors are not detected by the compiler, but rather exhibit themselves when the program executes.
As such, these errors are known as execution-time errors. They could cause our program to crash—that is, terminate its execution abnormally—and give us an error message. Such an error is called a runtime error. However, logical errors often don’t give any obvious indication that something is wrong. This type of error is known as a silent error. That is, our program terminates normally, but its results are incorrect.
Silent logical errors are typically the most difficult to find and usually will be our main focus. However, in this first debugging interlude, we will spend more time with examples of syntax errors.
Get hands-on with 1200+ tech skills courses.