java - Java 尝试捕获异常
问题描述
我当前的代码块返回如下异常:
Exception occurred in API invocation A1-123 Fatal error
Caused by: A9-001 ColName is not found in TableName
但我想...
- 摆脱
A1 Exception
A9 Exception
直接显示- 不显示
A9 Exception
在Caused by
如何使异常看起来像这样?
Exception occurred in API invocation A9-001 ColName is not found in TableName
<no Caused by clause>
这是我的示例代码:
public Sample Method (Input input) throws AException
{
con = getSQLConnection();
try{
//do something
if(x==null){
throw new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
}
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
finally{
if(con!=null){
try{
con.close();
}
catch(Exception e){
logger().error(e);
throw new AException(e);
}
}
}
}
如果它是这样的,它会起作用吗:
try{
//do something
}
catch (Exception e){
logger().error(e);
throw new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
解决方案
我假设您想要捕获并传递的错误是AException
- 否则您所要求的将违反该方法的合同。catch
你可以通过这样的额外子句来实现你想要的。
try {
// whatever
}
catch (AException ae) {
throw ae;
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
finally {
// whatever
}
这样,只有尚未属于类型的异常AException
才会被包装在新AException
对象中。
推荐阅读
- notepad++ - Notepad++ 使用记事本重新打开时会丢失换行符
- google-cloud-platform - Google Cloud Profiler Java 代理的日志记录级别
- r - 有没有办法将 CSS ID 添加到 R FastRWeb otable() 输出,以便可以使用 DatablesJS 对其进行格式化?
- version-control - 在 Mercurial 中,有没有办法指定一个文件只保留它的当前版本?
- amazon-web-services - AWS SDK .NET 的问题
- web-services - Xamarin.Android 在调用 Web 服务时处理连接失败
- asp.net-core-mvc - asp.net 核心身份页面(Razor 类库) - 需要西班牙语
- spring-boot - jhipster中如何集成ldap连接
- node.js - node.js 的活动管理类型包
- wordpress - WordPress - og:image 打开图形以获取作者头像