1.返回结果和异常如何选择
有数据需要回滚就抛出异常,否则返回结果
2.异常处理:
不是:
log.error("IO exception", e);
throw new MyException(e);
而是:
log.error("IO exception", "亨通接口出问题了,"+e.getMessage());
throw new MyException(1, "请求繁忙,请稍候再试!");
1.如何优雅处理异常
1.1 为什么不建议用 try catch?:这篇文章不错,没有什么不建议使用try catch,而是应该如何在正确使用try catch:https://www.zhihu.com/question/29459586/answer/1910031749
1.2 异常如何与断言结合处理,部分异常可使用断言替代,方便,简洁(spring使用断言和异常一同抛出异常,多学习)
1.2.1 什么是断言:https://blog.csdn.net/qq_43567126/article/details/114776369
3.项目如何统一处理异常(待实践)
4.spring如何处理异常
例:XmlBeanDefinitionReader.class
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
Assert.notNull(encodedResource, "EncodedResource must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Loading XML bean definitions from " + encodedResource);
}
Set currentResources = this.resourcesCurrentlyBeingLoaded.get();
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
}
try (InputStream inputStream = encodedResource.getResource().getInputStream()) {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
如何优雅处理异常、项目如何统一处理异常:https://zhuanlan.zhihu.com/p/68536207
springmvc、springboot统一处理异常、统一返回结果:https://blog.csdn.net/qq_42046105/article/details/123243661、https://blog.csdn.net/chinaherolts2008/article/details/120383594、https://zhuanlan.zhihu.com/p/28772817
gateway也加入进来了统一结果返回:Spring Cloud 统一异常处理和统一返回 - 微笑不加冰 - 博客园
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)