本指南将指导您完成如何在Java项目中创建自定义异常。
以下是创建自定义例外的步骤:
以下是总结:
让我们创建一个名为 ResourceNotFoundException 的自定义异常。通常,*ResourceNotFoundException * 自定义异常在未找到请求的资源时由服务层抛出。
现在,我们将通过示例讨论如何创建选中和未选中的自定义异常。
如果客户端能够从异常中恢复,则将其设置为选中异常。
public class ResourceNotFoundException extends Exception {
private static final long serialVersionUID = 1L;
public ResourceNotFoundException(Object resourId) {
super(resourId != null ? resourId.toString() : null);
}
}
如何在应用程序中使用 ResourceNotFoundException 自定义异常?
public class TestResourceNotFoundException {
public static void main(String[] args) throws ResourceNotFoundException {
ResourceManager manager = new ResourceManager();
manager.getResource(0);
}
}
class Resource {
private int id;
public Resource(int id) {
super();
this.id = id;
}
}
class ResourceManager {
public Resource getResource(int id) throws ResourceNotFoundException {
if (id == 10) {
new Resource(id);
} else {
throw new ResourceNotFoundException("Resource not found with id ::" + id);
}
return null;
}
}
class ResourceNotFoundException extends Exception {
private static final long serialVersionUID = 1L;
public ResourceNotFoundException(Object resourId) {
super(resourId != null ? resourId.toString() : null);
}
}
输出:
Exception in thread "main" com.ramesh.corejava.devguide.exceptions.customexceptions.ResourceNotFoundException: Resource not found with id ::0
at com.ramesh.corejava.devguide.exceptions.customexceptions.ResourceManager.getResource(TestResourceNotFoundException.java:26)
at com.ramesh.corejava.devguide.exceptions.customexceptions.TestResourceNotFoundException.main(TestResourceNotFoundException.java:6)
如果客户端无法从异常中恢复,则将其设置为unchecked异常。
public class BaseRuntimeException extends RuntimeException {
private static final long serialVersionUID = 1L;
private int statusCode = 500;
public BaseRuntimeException() {
super();
}
public BaseRuntimeException(String message) {
super(message);
}
public BaseRuntimeException(BaseException be) {
super(be.getMessage(),be);
this.statusCode = be.getErrorCode();
}
public BaseRuntimeException(Throwable t) {
super(t.getMessage(),t);
}
public BaseRuntimeException(String message, Throwable t) {
super(message, t);
}
public BaseRuntimeException(int status, String message, Throwable t) {
super(message, t);
this.statusCode = status;
}
public BaseRuntimeException(String message, Throwable t,
boolean enableSuppression, boolean writableStackTrace) {
super(message, t, enableSuppression, writableStackTrace);
}
public int getStatusCode() {
return statusCode;
}
}
如何在应用程序中使用 BaseRuntimeException 自定义异常。
public class TestRunTimeException {
public static void main(String[] args) {
List<String> data = new ArrayList<>(Collections.nCopies(100, "Ramesh"));
processData(data);
}
public static void processData(List<String> data){
if(data.size() > 50){
throw new BaseRuntimeException("Image file size con't exceed :: " + 10000000);
}
}
}
输出:
Exception in thread "main" com.ramesh.corejava.devguide.exceptions.customexceptions.BaseRuntimeException: Image file size con't exceed :: 10000000
at com.ramesh.corejava.devguide.exceptions.customexceptions.TestRunTimeException.processData(TestRunTimeException.java:15)
at com.ramesh.corejava.devguide.exceptions.customexceptions.TestRunTimeException.main(TestRunTimeException.java:10)
我个人在日常项目工作中使用的自定义异常很少。
public class BadRequestException extends Exception {
private static final long serialVersionUID = 1L;
public BadRequestException() {
super();
}
public BadRequestException(String message) {
super(message);
}
public BadRequestException(String message, Throwable t) {
super(message, t);
}
public BadRequestException(Throwable t) {
super(t);
}
}
找不到请求资源时,服务层引发ResourceNotFoundException自定义异常
public class ResourceNotFoundException extends Exception {
private static final long serialVersionUID = 1L;
public ResourceNotFoundException(Object resourId) {
super(resourId != null ? resourId.toString() : null);
}
}
当请求参数错误或用户未授权时,服务层会抛出UnauthorizedRequestException自定义异常。
public class UnauthorizedRequestException extends Exception {
private static final long serialVersionUID = 1L;
public UnauthorizedRequestException(String message) {
super(message);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/06/guide-to-create-custom-exceptions.html
内容来源于网络,如有侵权,请联系作者删除!