DEV Community

Cover image for Common Exceptions In Spring Core and SpringMVC
Shikha Verma
Shikha Verma

Posted on

Common Exceptions In Spring Core and SpringMVC

Spring Core and Spring MVC throw various exceptions based on different scenarios.

1. Exceptions in Spring Core

Spring Core mainly deals with dependency injection, bean management, and context configuration. The following are common exceptions:

a. Bean Creation & Configuration Exceptions

  1. BeanCreationException – Thrown when a bean cannot be created due to issues like circular dependencies or missing dependencies.

  2. BeanDefinitionStoreException – Raised when there is an issue with bean definition parsing (e.g., incorrect XML config).

  3. NoSuchBeanDefinitionException – Thrown when requesting a bean that is not found in the Spring container.

  4. BeanInitializationException – Occurs when a bean fails to initialize (e.g., @PostConstruct method throws an exception).

  5. UnsatisfiedDependencyException – Raised when a required dependency is missing in the Spring container.

  6. ScopeNotActiveException – Occurs when a bean is requested in an inactive scope (e.g., requesting a session-scoped bean in a non-web context).

b. Application Context & Dependency Injection Exceptions

  1. ApplicationContextException – Generic exception related to application context issues.

  2. ConversionNotSupportedException – Raised when Spring fails to convert a bean property to the required type.

  3. TypeMismatchException – Occurs when a bean property type does not match the expected type.

  4. MethodInvocationException – Thrown when a method invocation on a bean fails.

c. Transaction Management Exceptions

  1. DataAccessException – Parent class for all exceptions in Spring JDBC and ORM modules.

  2. CannotCreateTransactionException – Thrown when a transaction cannot be started.

  3. TransactionSystemException – Occurs due to unexpected failures in transaction processing.

  4. TransactionUsageException – Raised when a transaction is misused (e.g., trying to commit a rolled-back transaction).

d. AOP & Proxy-related Exceptions

  1. AopInvocationException – Thrown when an aspect method fails.

  2. AspectJWeavingException – Occurs due to issues in AspectJ weaving (e.g., incorrect pointcut expressions).

  3. ProxyInstantiationException – Raised when Spring fails to create a proxy for a bean.

2. Exceptions in Spring MVC

Spring MVC handles web-based request processing, controllers, and view rendering. The following are common exceptions:

a. HTTP Request & Response Handling Exceptions

  1. HttpRequestMethodNotSupportedException – Raised when a requested HTTP method (e.g., POST) is not supported by the controller.

  2. HttpMediaTypeNotSupportedException – Occurs when the request contains an unsupported Content-Type.

  3. HttpMediaTypeNotAcceptableException – Raised when the requested media type in Accept header is not supported.

  4. MissingServletRequestParameterException – Thrown when a required request parameter is missing.

  5. ServletRequestBindingException – Occurs when Spring cannot bind request parameters to method arguments.

b. Path & Handler Mapping Exceptions

  1. NoHandlerFoundException – Raised when no handler (controller) is found for a request URI.

  2. HandlerMappingIntrospectorException – Occurs due to issues in handler mapping introspection.

  3. NoSuchRequestHandlingMethodException – Thrown when no matching handler method is found for the request.

c. Data Binding & Validation Exceptions

  1. BindException – Raised when binding request parameters to a model object fails.

  2. MethodArgumentNotValidException – Thrown when validation fails on @valid annotated request body or parameters.

  3. TypeMismatchException – Occurs when request parameter conversion fails (e.g., trying to convert abc to an integer).

d. Controller Advice & Exception Handling

  1. ResponseStatusException – Generic exception that allows setting an HTTP status code along with a message.

  2. ConversionFailedException – Thrown when a type conversion fails during request processing.

  3. MissingPathVariableException – Raised when a path variable required in a controller method is missing.

  4. MissingServletRequestPartException – Occurs when a required @RequestPart (e.g., file upload) is missing.

e. Session & Security-related Exceptions

  1. SessionRequiredException – Raised when an operation requires an active session, but none exists.

  2. AuthenticationException – Parent class for all authentication-related exceptions in Spring Security.

  3. AccessDeniedException – Thrown when a user tries to access a resource they do not have permission for.

.................
This is a broad categorization of exceptions in Spring Core and Spring MVC. Let me know if you need detailed explanations for any specific ones!

Do Follow!! ❤️

Top comments (0)