gpt4 book ai didi

java - 从java中的异常处理e中检索值

转载 作者:行者123 更新时间:2023-11-30 08:26:05 26 4
gpt4 key购买 nike

我有兴趣检查异常值,如果我将状态设置为特定值,那么我想将货币字符串返回/设置为相应的错误,用户可以在客户端抛出错误。

/*code snippet */

cr.setName(Name);
cr.setContact(User);
cr.setValue(Value);
cr.setStatus(status);

try
{
currency = (serviceCountry.createCurrencyTicket( cr, null ));

if ( testPattern( currency ) )
{
return currency;
}

}
catch ( Exception e )
{
this.logger.log( Level.WARNING,
"Exception occured while evaluating currency = "
+ currency, params );
}

例如Exception e有response、cause、stackTrace对象。我想从响应中检索值并将货币设置为特定错误。有点像

catch ( Exception e )
{
if ( e.getClass().getName().getStatus() == 2 )
{
currency = 'ERROR';
} else if ( e.getClass().getName().getStatus() == 0 )
{
currency = 'DOWN';
}
}

最佳答案

捕获抛出的实际异常类型是一种选择

try
{
currency = (serviceCountry.createCurrencyTicket( cr, null ));
}catch(MyCustomException ex)
{
if( ex.getStatus() == 2 )
currency = "Oh Nos";
else if( ex.getStatus() == 0 )
currency = "Ehh";
}catch(Exception e)
{
currency = "SuperBad";
}

另一个虽然乏味的选择是检查抛出的异常的类型

try
{
currency = (serviceCountry.createCurrencyTicket( cr, null ));
}catch(Exception e)
{
if( e instanceof MyCustomException )
{
MyCustomException customEx = (MyCustomException)e;
if( customEx.getStatus() == 2 )
currency = "Oh Nos";
else if( customEx.getStatus() == 0 )
currency = "Ehh";
}
else
currency = "SuperBad";
}

关于java - 从java中的异常处理e中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21787993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com