Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

Exception in AP Computer Science A

In AP Computer Science A, an exception is a type of run-time error. It happens while a program is running, it comes from a problem the compiler did not detect, and it interrupts the normal flow of execution, often stopping the program abnormally. An ArrayIndexOutOfBoundsException is a classic example.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is Exception?

An exception is what Java throws when something goes wrong while your program is running. Your code compiled fine, so the syntax was legal. Then, during execution, the program tried to do something impossible. Maybe it accessed array[5] in an array of length 5, divided an int by zero, or called a method on a null reference. Java can't continue normally, so it interrupts execution and the program usually crashes with a message like ArrayIndexOutOfBoundsException.

The CED files exceptions under run-time errors (EK 1.1.C.3 and 1.1.C.4). Think of the compiler as a spell-checker. It catches grammar mistakes (syntax errors) before the program runs. It can't predict that a loop will run one step too far, or that a variable will be null at runtime. Those problems only show up when the code actually executes, and that's exactly where exceptions live.

Why Exception matters in AP® Computer Science A

Exceptions show up in Topic 1.1 (Why Programming? Why Java?) in Unit 1: Using Objects and Methods. They directly support AP Comp Sci A 1.1.C, which asks you to identify types of programming errors (syntax, logic, run-time, and exceptions). They also tie to AP Comp Sci A 1.1.B, which asks you to explain compilation and execution. That's because the whole point of an exception is that it gets past the compiler (EK 1.1.B.2) and only appears at run time. Exceptions stay relevant long after Unit 1. Every time you write a loop over an array, call substring, or use an object reference, the exam can ask whether that code will throw one.

How Exception connects across the course

Syntax Errors and the Compiler (Unit 1)

Syntax errors and exceptions sit on opposite sides of the compile step. The compiler catches syntax errors before the program can run. Exceptions slip past the compiler and only blow up during execution. If a question says the code 'compiles successfully but terminates unexpectedly,' you're looking at an exception, not a syntax error.

Logic Errors (Unit 1)

Logic errors are often the cause, and the exception is the symptom. Writing i <= arr.length instead of i < arr.length is a logic mistake in your algorithm. When the loop reaches the extra index, Java throws an ArrayIndexOutOfBoundsException. Some logic errors crash the program this way. Others just quietly produce the wrong answer.

Arrays and ArrayList Traversal (Unit 4)

Off-by-one loop bounds are the number one source of exceptions on the exam. Valid array indexes run from 0 to length - 1. Step outside that range on an array or ArrayList and you get an index-out-of-bounds exception. ArrayList FRQs that remove elements during traversal, like the 2021 FRQ Q3 MemberInfo question, are prime territory for this kind of index slip.

Strings, Objects, and null (Units 1 and 3)

Exceptions aren't just an array thing. Calling substring with an index past the String's length throws a StringIndexOutOfBoundsException. Calling a method on a reference that holds null throws a NullPointerException. Any time code depends on a value that might be out of range or missing, an exception is possible.

Is Exception on the AP® Computer Science A exam?

Exceptions mostly show up in multiple-choice code-tracing questions. A typical stem gives you a code segment (often a for loop over an array) and asks what happens when it runs. The trap answer is a printed sum. The right answer is that an ArrayIndexOutOfBoundsException occurs because the loop uses <= instead of <. Other stems describe a scenario like 'the program compiles and begins execution, but terminates unexpectedly' and ask you to classify the error. You need to do three things. Trace the code carefully, spot the line that goes out of bounds or uses null, and correctly label the problem as a run-time error or exception rather than a syntax or logic error. On FRQs, no one asks you to define the term. Your code just has to avoid throwing exceptions. In ArrayList and array questions such as the 2021 FRQ Q3 (MemberInfo/club members), a sloppy index can crash your solution and cost you points. The AP exam focuses on recognizing and preventing exceptions, not on writing try/catch blocks.

Exception vs Logic error

A logic error is a mistake in your algorithm that makes the program behave incorrectly. You find it by testing with specific data. An exception is what happens when a mistake causes the program to hit an impossible operation at run time, so execution gets interrupted. The two overlap. An off-by-one loop is a logic error that causes an exception. But a logic error that just averages scores wrong never throws anything. It runs to the end and prints the wrong number. So ask yourself one question. Did the program crash mid-run? If yes, there's an exception. If it finished with a bad answer, it's a plain logic error.

Key things to remember about Exception

  • An exception is a type of run-time error that interrupts the normal flow of a program while it is executing.

  • Code that throws an exception still compiles successfully, because the compiler cannot detect the problem ahead of time.

  • Accessing index length in an array (for example array[5] in an array of length 5) throws an ArrayIndexOutOfBoundsException.

  • Dividing an int by zero, calling a method on a null reference, and using a bad substring index all throw exceptions too.

  • A logic error like using <= instead of < in a loop condition is often the root cause of an exception.

  • On FRQs, you are not asked to define exceptions, but your code must avoid triggering them, especially when traversing arrays and ArrayLists.

Frequently asked questions about Exception

What is an exception in AP Computer Science A?

An exception is a run-time error caused by a problem the compiler didn't detect, and it interrupts the program's normal execution. Common AP examples include ArrayIndexOutOfBoundsException, NullPointerException, and ArithmeticException from integer division by zero.

Does code with an exception fail to compile?

No. Code that throws an exception compiles just fine, because exceptions only happen during execution. If the compiler catches the problem, it's a syntax error, not an exception.

Is an exception the same as a logic error?

No, but they're related. A logic error is a flaw in your algorithm, and sometimes that flaw causes an exception, like a loop using i <= arr.length that crashes on the last pass. Other logic errors never throw anything and just produce wrong output.

How is an exception different from a syntax error?

A syntax error breaks Java's language rules and is caught by the compiler before the program can run. An exception happens after the code compiles, while it is running, and usually makes the program terminate abnormally.

Do I need to write try/catch for the AP CSA exam?

No. The exam expects you to recognize when code will throw an exception and to write code that avoids one, like keeping loop indexes between 0 and length - 1. Writing your own exception-handling blocks isn't part of what the exam asks you to do.