This tutorial will look at different types of errors that you can run into within Java and other computer languages.
Anyone who has spent time programming knows that errors are a part of the process. Understand the different types of errors can be a critical step to help debug the problem. In this tutorial, you are going to examine the basic types of errors: syntax errors, compiler errors, run-time errors, and logical errors. Each of the different types has different characteristics and being able to understand these characteristics can help the debug process. Let’s take a look at each of these one at a time.
The first type of error we are going to look at is a syntax error. The syntax of Java is the rules that you need to follow for a properly formatted code. Syntax errors are often spotted as you type. Depending on the IDE (integrated development environment) that you use, syntax errors are often either highlighted or noticed when keywords are not color-coded as expected.
In the code below, notice that the keyword System
is highlighted as a different color when it is correctly typed with a capital letter, but the color is missing when it is incorrectly typed with a lowercase s
.
Syntax errors can often get caught before attempting to compile and run the program, but if you do attempt to compile and run, the program will crash and fail to compile.
In addition to syntax errors that are not caught when writing the code, there are additional errors that the compiler will find causing your program to error before it runs. Each computer language has different types of things that are checked at the time of compiling, but here are a few examples of things that cause compiler errors in Java:
As you can see, most of these errors will not show up as incorrect syntax, but rather will cause an error once the compiler tries to create the program. The key to recognizing compiler errors is that the error will be produced and the code will not run.
The next error type to examine is run-time errors. As the name implies, these are errors that are only produced as the program runs. These errors have code that compiles correctly and may run correctly under some situations but does something either unexpected or incorrect. Examples of run-time errors can be a user entering a String when the program expects a number, looping too far and getting an index out of bounds error, etc.
Here are some examples in the following program. Notice that the program will compile and even run correctly under certain conditions, but other conditions will cause an error.
The last type of error that we will examine is a class known as Logic Errors. Logic errors are different from the other error types because these errors do not cause any type of error from Java. Programs with logic errors will compile, run, and finish successfully, however they will not produce the correct results.
Let’s take a look at the example above. The program asks the user for an input. If the user inputs the number 10, the program will print out zero since 5 / 10 = 0.5, but since both values are integers, the value gets truncated and the incorrect value is printed to the screen.
You can also see logic errors with if-then statements. See the example below.
Notice in the example above, the program has proper syntax, compiles without a problem, and executes to completion (as long as you enter an integer), but it doesn’t produce the correct result. This is because on line 12, the code uses an if statement that evaluates to true for even number, not odd numbers.
Debugging code can be a challenging task. By examining where and how a program produces an error, it can help you to figure out what that error may be. Syntax and compiler errors prevent the code from starting to run. Run-time errors occur as the program runs, and logical errors are problems that are found when the program runs to completion without error, but produces the wrong output.