scala
Scala Standard Exceptions
Exceptions and errors commonly thrown in Scala programs, from the Scala standard library (scala._) and the underlying JVM. Includes Scala-specific failures such as non-exhaustive pattern matches and lazy val initialisation cycles.
16 codes
· All codes 16 codes
- ArithmeticException ArithmeticException Thrown for arithmetic failures. The most common cause is integer division by zero (x / 0 or x % 0). Floating-point division by zero produces Infinity or NaN rather than an exception.
- AssertionError AssertionError Thrown when an assert(condition) call evaluates to false. Scala's assert is always active (unlike Java's, which requires -ea); use assert(cond, msg) to include a descriptive message.
- ClassCastException ClassCastException Thrown when asInstanceOf[T] is called on a value that is not an instance of T. Prefer pattern matching to avoid this; in well-typed code it usually indicates an incorrect assumption about a value received from an external source.
- ConcurrentModificationException ConcurrentModificationException Thrown when a Java iterator detects that the underlying collection was modified during iteration. Most Scala collections are immutable and not susceptible; this typically occurs when using mutable Java collections from Scala.
- IllegalArgumentException IllegalArgumentException Thrown when a method receives an argument that violates its precondition. In Scala, require(condition, message) is the idiomatic way to validate arguments and throws IllegalArgumentException when the condition is false.
- IllegalStateException IllegalStateException Thrown when a method is called and the object is in an inappropriate state for that operation. Scala Builder-pattern classes use this for state violations.
- IndexOutOfBoundsException IndexOutOfBoundsException Thrown when a sequence is accessed at an index that is negative or greater than or equal to its length. Applies to Array, Vector, List, and other indexed collections.
- MatchError MatchError Thrown when a match expression is evaluated against a value not covered by any of its cases. Indicates a non-exhaustive pattern match; use a wildcard case or a sealed class hierarchy to prevent this at runtime.
- NoSuchElementException NoSuchElementException Thrown when accessing an element that does not exist, such as calling head or last on an empty collection, or calling get on a None. In Scala, Option.get is the most common cause.
- NotImplementedError NotImplementedError Thrown when the stub expression ??? is evaluated. Used to mark methods that have not yet been implemented; evaluating any ??? at runtime always throws this error unconditionally.
- NullPointerException NullPointerException Thrown when a null reference is dereferenced. In idiomatic Scala this is avoided by using Option instead of null; however, interop with Java libraries can still introduce null values.
- NumberFormatException NumberFormatException Thrown when a string cannot be parsed into a numeric type, such as "abc".toInt. Use Try(s.toInt) or s.toIntOption to handle conversion failures without exceptions.
- OutOfMemoryError OutOfMemoryError Thrown when the JVM cannot allocate more memory in the heap. Causes include very large collections, accumulated objects without garbage collection, or an insufficient -Xmx setting.
- StackOverflowError StackOverflowError Thrown when the JVM call stack depth is exceeded, almost always due to infinite or excessively deep recursion. Methods annotated with @tailrec are compiled to loops and avoid this; ensure tail-call optimisation is applied for deeply recursive code.
- UninitializedFieldError UninitializedFieldError Thrown when a lazy val is accessed during its own initialisation, creating a cycle. Scala detects this cycle and throws UninitializedFieldError rather than looping indefinitely.
- UnsupportedOperationException UnsupportedOperationException Thrown when an operation is not supported — most often when a mutating method is called on an immutable collection, or when a method is declared but deliberately not implemented.