Junior — Middle
How can you catch a UserNotFoundException and redirect the user to the registration page?
sobes.tech AI
Answer from AI
To catch a UserNotFoundException in Java, a try-catch block is usually used. In the catch block, you can redirect the user to the registration page.
Example:
try {
userService.login(username, password);
} catch (UserNotFoundException e) {
// Redirect to registration page
response.sendRedirect("/register");
}
If it is a web application in Java (for example, using servlets), then response.sendRedirect() will send the user to the desired URL.
In desktop or mobile applications, redirection may mean calling the appropriate method to open the registration screen.
It is important to handle the exception where it is possible to correctly change the control flow and show the user the necessary interface.