Course contentsShow
← Java
Lesson 15 of 2,8701. Java Setup and First ProgramFree lesson

Understanding Runtime Errors

Recognizing runtime errors and exceptions that occur when executing a Java program with the java command.

Understanding Runtime Errors

What you'll learn: You'll discover what runtime errors are and how they differ from the compilation errors you've already seen.

What Are Runtime Errors?

When you successfully compile your Java program with javac, you get a .class file—your code is syntactically correct. But when you actually run it with the java command, things can still go wrong. Runtime errors (also called exceptions) are problems that occur while your program is executing.

Think of it like baking: compilation errors are like catching a mistake in the recipe before you start (missing an ingredient). Runtime errors are problems that happen during baking—the oven breaks, or you run out of eggs halfway through.

Common Runtime Error Examples

Even though your Hello World program compiled fine, runtime errors can happen when:

  • You run the wrong class name: java HelloWorld when your file contains class HelloWorld works, but java HelloWrld (typo) will fail at runtime with "Error: Could not find or load main class"
  • The main method is missing or misspelled: If you wrote public static void Main instead of public static void main, it compiles but crashes when you run it

What You'll See

Runtime errors display error messages in your terminal, often starting with words like "Exception" or "Error." Unlike compilation errors that prevent the .class file from being created, runtime errors mean your program started but couldn't continue.

The JVM tries to execute your code but encounters something it can't handle, so it stops and tells you what went wrong.

Key Takeaway: Runtime errors occur when executing a valid .class file with java, meaning your syntax was correct but something went wrong during execution—like using the wrong class name or having an incorrect main method signature.