ReviseAlgo Logo

Exception Handling

throw Keyword

Deconstruct explicit exception throwing, stack trace capture costs, and flow control limits.

Interview: Evaluates the execution overhead of instantiating Exception objects (fillInStackTrace) and when to throw custom exceptions vs standard ones.

Last Updated: June 13, 2026 10 min read

The throw keyword is used to explicitly throw an exception from a method or block. While syntactically simple, throwing exceptions introduces significant performance overhead due to the creation of stack traces.

Explicit Throw

Used as throw instance; to delegate exception propagation to callers. It halts the normal execution path immediately.

Stack Capture Cost

Instantiating a Throwable captures the current stack trace via a native JVM call (fillInStackTrace), which has high performance overhead.

Flow Control

Exceptions are not GOTO statements. They should only be used to handle abnormal system states, not for normal validation paths.

The Cost of Stack Trace Capture

When an exception is thrown in Java, the JVM performs a native call:

  • fillInStackTrace(): This method walks the current execution stack frames, capturing details (class name, method name, file name, line number) to build the stack trace array.
  • Performance Impact: This walk is computationally expensive. Throwing thousands of exceptions per second can degrade application performance, making exceptions poorly suited for normal flow control.

Common Pitfalls

  • Throwing Null References: Writing throw null;. Instead of throwing the intended exception, the JVM throws a NullPointerException immediately.
  • Throwing Exceptions for Validation: Using exceptions to validate routine user input (e.g. checking empty text fields), which degrades performance compared to basic conditional checks.

Best Practices

  • Validate Parameters Early: Throw standard exceptions (like IllegalArgumentException) at the start of a method to enforce API invariants.
  • Reuse Exceptions: For performance-critical code where stack traces are not needed, reuse static, pre-allocated exception instances with overridden, empty stack trace methods.

Interview-Relevant Information

Q1: Why is throwing exceptions considered performance-heavy in Java?
Answer: Because instantiating a Throwable invokes the native fillInStackTrace() method. This method walks the thread's execution stack frames to populate metadata (lines, files, methods), which has significant CPU overhead.

Q2: What happens if you execute the statement: throw null;?
Answer: The JVM will immediately throw a NullPointerException at runtime because you cannot throw a null reference.

Quick Checklist

Can you explain the performance cost of fillInStackTrace(), identify the result of throwing a null reference, and describe why exceptions are inappropriate for routine flow control? If yes, you understand the throw keyword.

Use Cases

Enforcing input validation rules at the boundaries of public APIs.

Throwing exceptions inside factory constructors to prevent objects from being created in invalid states.

Common Mistakes

Executing throw null, resulting in an unintended NullPointerException.

Using exception throwing to control standard loop exits, degrading application performance.