简体   繁体   English

从Java中的异常处理e检索值

[英]Retrieve value from exception handling e in java

I'm interested to check for exception value and if i have status set to specific value then would like to return / set currency string to respective ERROR, which can be user to throw error in client side. 我有兴趣检查异常值,如果我将状态设置为特定值,则想将货币字符串返回/设置为相应的错误,这可以使用户在客户端抛出错误。

/*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 );                
            }

for example, Exception e has response, cause, stackTrace object. 例如,异常e具有response,cause和stackTrace对象。 i would like to retrieve value from response and set currency to specific ERROR. 我想从响应中检索值并将货币设置为特定的错误。 Something like 就像是

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

Catching the actual type of Exception thrown is one option 捕获抛出的异常的实际类型是一种选择

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";
}

Another, although tedious option, would be to check the type of the thrown Exception 另一个虽然乏味的选择,但将是检查抛出的Exception的类型。

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM