BackendBench
Coffee
Reference

Java exceptions reference

What 40 common Java, Spring and Hibernate exceptions mean, why they happen and how to fix them, each with a broken and fixed example.

Unchecked exceptions (20)

Programming and data errors you are not forced to catch.

NullPointerExceptionjava.langA null reference was used where an object was needed: a method call, field access, unboxing or array access on null.ArrayIndexOutOfBoundsExceptionjava.langAn array was accessed with an index below zero or at or beyond its length.StringIndexOutOfBoundsExceptionjava.langA String method such as charAt or substring was given an index outside the string.IndexOutOfBoundsExceptionjava.langA List or other indexed collection was accessed with an index outside its size.ClassCastExceptionjava.langAn object was cast to a type it is not an instance of.NumberFormatExceptionjava.langA string could not be parsed as a number, usually by Integer.parseInt or Double.parseDouble.IllegalArgumentExceptionjava.langA method received an argument that is not acceptable, as decided by the method itself.IllegalStateExceptionjava.langA method was called at a time when the object was not in a state that allows it.ConcurrentModificationExceptionjava.utilA collection was structurally modified while it was being iterated.UnsupportedOperationExceptionjava.langAn optional operation, usually a modification, was called on a collection that does not support it.ArithmeticExceptionjava.langAn integer division or modulo by zero, or an inexact BigDecimal result with no rounding mode.NoSuchElementExceptionjava.utilAn element was requested from an empty or exhausted source: Optional.get, Iterator.next, Scanner or a collection.DateTimeParseExceptionjava.time.formatA date or time string did not match the expected pattern or contained an invalid value.RejectedExecutionExceptionjava.util.concurrentAn executor refused a task because it was shut down or its queue and threads were full.DataIntegrityViolationExceptionorg.springframework.daoSpring's translation of a database integrity constraint failure such as a duplicate key or a NOT NULL violation.LazyInitializationExceptionorg.hibernateA lazily loaded association or collection was accessed after the Hibernate session had closed.NoSuchBeanDefinitionExceptionorg.springframework.beans.factorySpring could not find a bean of the requested type or name to inject.NoUniqueBeanDefinitionExceptionorg.springframework.beans.factoryMore than one bean matches an injection point and Spring cannot choose between them.BeanCurrentlyInCreationExceptionorg.springframework.beans.factoryTwo or more beans depend on each other, forming a circular dependency that Spring cannot resolve.HttpMessageNotReadableExceptionorg.springframework.http.converterSpring MVC could not read the request body: invalid JSON, missing body or a type mismatch.

Checked exceptions (14)

Recoverable conditions the compiler makes you handle.

ClassNotFoundExceptionjava.langA class was requested by name at run time (Class.forName, a driver, a config file) but is not on the classpath.FileNotFoundExceptionjava.ioA file could not be opened: it does not exist, is a directory, or the process lacks permission.SocketTimeoutExceptionjava.netA network read or connect took longer than the configured timeout.ConnectExceptionjava.netThe TCP connection was refused or could not be made: nothing is listening, or a firewall rejected it.UnknownHostExceptionjava.netA host name could not be resolved to an IP address.SSLHandshakeExceptionjavax.net.sslThe TLS handshake failed: untrusted certificate, wrong host name, expired certificate or no shared protocol.InterruptedExceptionjava.langA thread blocked in sleep, wait or a blocking queue was interrupted by another thread.TimeoutExceptionjava.util.concurrentA blocking call with a time limit, such as Future.get(timeout), did not finish in time.ExecutionExceptionjava.util.concurrentA task run through an executor failed; the real exception is the cause.InvocationTargetExceptionjava.lang.reflectA method invoked through reflection threw an exception; the real error is the cause.SQLIntegrityConstraintViolationExceptionjava.sqlA SQL statement violated a database constraint: unique key, primary key, foreign key or NOT NULL.MethodArgumentNotValidExceptionorg.springframework.web.bindA @Valid request body failed Bean Validation: one or more constraints were violated.SQLTransientConnectionExceptionjava.sqlHikariCP could not give out a connection within the timeout: the pool is exhausted or the database is unreachable.UnrecognizedPropertyExceptioncom.fasterxml.jackson.databind.excJackson found a JSON field that has no matching property in the target Java class.

Errors (6)

Serious JVM and environment problems.

All kinds: Checked exception, Unchecked exception, Error. Package names are shown under each exception.