Coding Rooms transition to “read-only” state on August 31, 2024  Click here for more information.

Runtime Errors Java

Learn how to handle runtime errors in Java

October 8, 2020
by
Sasha Varlamov

There are three types of errors that programmers frequently encounter. Let’s examine the first one, runtime error or runtime exceptions, by looking at the example below.

RunTimeErrorExample.java
package exlcode;

public class RunTimeErrorExample {

  public static int exampleVariableOne = 5;
  public static int exampleVariableTwo = exampleVariableOne/0;

  public static void main(String[] args) {
    // this creates an error because numbers cannot be divided by zero
    System.out.println(exampleVariableTwo);
  }
}

When we run the code above, an error occurs and nothing is printed out onto the console. Runtime errors happen after the code is compiled and when the program runs the code. Because these errors cause the program to crash, they are hard to track down. In these cases, using comments to group off parts of the source code to narrow down the location of the error is a useful approach.

In the example above, we encounter a runtime error because the program is unable to divide by zero. In these types of errors, the program reaches something illegal, something it is unable to understand, or if it’s unable to find the code.

Sasha Varlamov
Sasha Varlamov

Coding Rooms
Founder and CEO

October 8, 2020

Learn why these organizations choose Coding Rooms as their next-generation learning platform.