Lesson Overview and Goals

Welcome, dear coder! Today, we're going to tackle error messages in Java. Consider these messages as signposts, guiding us to fix glitches in our Java program. Let's go!

Introduction to Error Messages

Error messages in Java are crucial signals of problems in our code. Typically, an error message contains three key parts: the type of error, the location of the error (class name and line number), and a brief error description.

Let's examine the following:

public class Solution {
    public static void main(String[] args) {
        System.out.println("Hello, space travelers);
    }
}

In this case, the compiler trips over a small error — a missing closing quotation mark. The error message provides our first clue about what's going wrong. The error will be:

Solution.java:3: error: unclosed string literal
        System.out.println("Hello, space travelers);
                           ^
1 error

See? The error mentions the file name, the line where the error happens, as well as the cause itself - the string literal (") is not closed.

Compilation Errors in Java

Every Java program is executed in two steps - first, it compiles code, generating a machine-readable format that is called bytecode (located in .class files), and once the code is compiled - it is executed.

Compile-time errors appear when our Java file is being compiled. These are typically syntax errors — like unclosed string literals and missing semicolons. Let's revisit our previous error:

Solution.java:3: error: unclosed string literal
        System.out.println("Hello, space travelers);
                           ^
1 error

The error message indicates an unclosed string literal issue on line 3 in Solution.java.

Runtime Errors in Java

Runtime errors occur after your code has successfully compiled, they occur while your program is executing. So runtime error means that your code is syntactically correct, but it still wasn't able to correctly execute, and the error occurred while running the compiled code.

Examples of runtime errors include dereferencing null values or attempting to access out-of-bound array elements (there are more; these two are just the most common examples). Let's inspect an example:

public class Solution {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        System.out.println(arr[5]);
    }
}

This program results in an ArrayIndexOutOfBoundsException:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3
    at Solution.main(Solution.java:4)

This message shows that we attempted to access index 5 in an array of length 3. Doing so caused a runtime error at line 4 in Solution.java.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal