Created
August 22, 2025 13:56
-
-
Save sachin-handiekar/2a72e3ce8fb506794f3d94a3b67e025a to your computer and use it in GitHub Desktop.
CustomExceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.resttemplatedemo.exception; | |
public class CustomExceptions { | |
public static class ApiException extends RuntimeException { | |
private final int statusCode; | |
private final String responseBody; | |
public ApiException(String message, int statusCode, String responseBody) { | |
super(message); | |
this.statusCode = statusCode; | |
this.responseBody = responseBody; | |
} | |
public int getStatusCode() { | |
return statusCode; | |
} | |
public String getResponseBody() { | |
return responseBody; | |
} | |
} | |
public static class BadRequestException extends ApiException { | |
public BadRequestException(String message, String responseBody) { | |
super(message, 400, responseBody); | |
} | |
} | |
public static class UnauthorizedException extends ApiException { | |
public UnauthorizedException(String message, String responseBody) { | |
super(message, 401, responseBody); | |
} | |
} | |
public static class ForbiddenException extends ApiException { | |
public ForbiddenException(String message, String responseBody) { | |
super(message, 403, responseBody); | |
} | |
} | |
public static class NotFoundException extends ApiException { | |
public NotFoundException(String message, String responseBody) { | |
super(message, 404, responseBody); | |
} | |
} | |
public static class InternalServerErrorException extends ApiException { | |
public InternalServerErrorException(String message, String responseBody) { | |
super(message, 500, responseBody); | |
} | |
} | |
public static class ServiceUnavailableException extends ApiException { | |
public ServiceUnavailableException(String message, String responseBody) { | |
super(message, 503, responseBody); | |
} | |
} | |
public static class TimeoutException extends RuntimeException { | |
public TimeoutException(String message, Throwable cause) { | |
super(message, cause); | |
} | |
} | |
public static class NetworkException extends RuntimeException { | |
public NetworkException(String message, Throwable cause) { | |
super(message, cause); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment