WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Spring Framework Β· Intermediate Β· question 32 of 100

Explain the difference between checked and unchecked exceptions in Spring transaction management.?

πŸ“• Buy this interview preparation book: 100 Spring Framework questions & answers β€” PDF + EPUB for $5

Checked and unchecked exceptions in Spring transaction management relate to how transactions are handled when an exception occurs.

Checked exceptions are exceptions that must be declared in the method signature or handled using a try-catch block. These exceptions are checked by the compiler at compile-time, and include exceptions like IOException and SQLException. When a checked exception occurs in a transactional method, the transaction must be rolled back by default (unless the exception is explicitly not marked as rollback-able).

On the other hand, unchecked exceptions are exceptions that do not need to be declared in the method signature or handled using a try-catch block. These exceptions are not checked by the compiler at compile-time, and include exceptions like NullPointerException and ArrayIndexOutOfBoundsException. When an unchecked exception occurs in a transactional method, the transaction is rolled back by default.

Spring transaction management provides features that allow you to customize how transactions are handled in the event of an exception, including options like marking certain exceptions as non-rollback-able or specifying a rollback policy for specific exceptions.

Here is an example of how checked and unchecked exceptions might be handled differently in a Spring transactional method:

@Transactional
public void updateUser(User user) throws SQLException {
    // Update user in database here
    // If there is a SQLException, the transaction will be rolled back by default
}
@Transactional
public void doSomething() {
    String str = null;
    // This will cause a NullPointerException, which will be treated as an unchecked exception
    // The transaction will be rolled back by default
    System.out.println(str.length());
}
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview β€” then scores it.
πŸ“ž Practice Spring Framework β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Spring Framework questions & answers β€” PDF + EPUB for $5

All 100 Spring Framework questions Β· All topics