Junior — Middle
What type of exception would you use to handle a situation where it is impossible to establish a connection with a microservice in a booking system?
sobes.tech AI
Answer from AI
To handle situations where it is impossible to establish a connection with a microservice in the booking system, network-related exceptions are usually used. In Java, these can be:
IOExceptionor its subclasses, if standard input-output is used for network operations.- Specialized exceptions, such as
ConnectException, which indicates a failure to establish a connection.
If an HTTP client (e.g., HttpClient or RestTemplate) is used in the project, there may be specific exceptions, such as ResourceAccessException in Spring.
For more precise handling, you can create a custom exception, such as ServiceUnavailableException, which signals that the microservice is unavailable:
public class ServiceUnavailableException extends RuntimeException {
public ServiceUnavailableException(String message) {
super(message);
}
}
Thus, when a connection cannot be established, this exception can be thrown and handled at the business logic level.