Programming errors are flaws in how applications work. They are commonly referred to as "bugs", hence the term “debugging”.

As a developer, you'll find yourself spending a lot of time fixing bugs. A number of the errors you will meet are common, and knowing them will help you prevent them in the first place.

Here's what you need to know on these three types of programming errors and how you can safeguard against them:

1. Runtime or Execution Errors

These are errors that occur when a program is executing (i.e. at runtime). They may cause a program to not execute properly or even not run at all.

Fatal runtime errors cause program execution to stop while the non-fatal ones cause execution to finish, but with incorrect results.

A typical runtime error is a division by zero error. Division by zero is supposed to yield an infinite result, but unfortunately, we haven't come up with a data structure that can store that amount of data yet.

Therefore, division by zero leads to an arithmetic exception in the Java compiler.

2. Logic Errors

Logic errors are caused due to flawed reasoning. It's important to note that these errors are not necessarily due to a “mistake” you’ve made. They may occur because you didn’t consider a certain execution scenario.

They are the most difficult to handle. This is because code with a logic error is a valid program in the language it's written in. Therefore, it won't throw a compiler error. The only issue is that it produces incorrect results.

A fatal logic error will cause program execution to stop while a nonfatal one will allow program execution to continue but with incorrect results.

A common logic error is an off-by-one error. This normally occurs when stating a loop-continuation condition. Say you want to print out the first five square numbers.

Related: Most Common Programming and Coding Mistakes

You might end up writing the code below in your for loop, which gives only the first four such numbers.

        for( int x=1; x<5; x++){ System.out.ln(x*x); }
    

To avoid such an error, you could instead use the <= sign. Using the less-than-or-equal-to sign is more intuitive and you’ll therefore be less likely to mix up your relational operations.

Another common logic error is leaving out both braces of a control statement and yet the body below forms a block of code that is under its control.

Look at the example below. It checks if a random number is odd or even, then prints an output.

        import java.util.Random;

public class OddEven{
public static void main(String[] args) {
Random numberGenerator = new Random();
int randomNumber = numberGenerator.nextInt(10);

if ((randomNumber%2)==0)
System.out.println("Here is your lucky number :" + randomNumber);
System.out.println("The number "+ randomNumber +" that you got is even"); // line 11
}
}

Notice Line 11. It'll always execute regardless of whether the random number you got is even. This would of course be logically wrong when the number you got is odd.

Including both System.out.println statements between braces { }, would have avoided this.

Related: Software Development Steps All Programmers Should Know

Another logic error to look out for is not providing a loop termination condition. This will result in an infinite loop and your program will never finish execution.

3. Syntax or Compile-Time Errors

These are errors caused due to violations of Java's language rules. They're also called compilation or compile-time errors.

These are the easiest errors to handle because your compiler will always report them. Many compilers even go ahead and tell you the line in your code the error is on.

Fault Tolerance

A practical way of dealing with software issues is to emlpoy fault tolerance by including exception handling. You can use try..catch statements to achieve this.

To continue with program execution regardless of the exception caught in the catch block, use the finally statement.

The syntax is:

        try{ // Block to execute if there are no issues } 
catch (Exception e){
// Block to handle issues found
}finally{ // Block to execute after catch
}

See the code example below:

        import java.util.Random;

public class RandomNumbers{
public static void main(String[] args) {
Random numberGenerator = new Random();

try{
for (int counter = 10; counter<=100; counter++){
int randomNumber = numberGenerator.nextInt(10);
System.out.println(counter/randomNumber); } }
catch(Exception e){
System.out.println("Division by zero encountered!");
}
finally{
System.out.println("Infinite value got");}
}
}

The above program generates a random number between zero and 10, and then uses that number to divide a counter value between 10 and 100. If division by zero is encountered, the system catches the error and displays a message.

Get Better at Coding

It's good practice to add comments to your code. This will help you easily comb through your files when you have a bug. One small, but very important step to developing strong coding practices.

With good coding practices, you should be able to avoid common programming mistakes.