Question

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. 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';
   }
}
Was it helpful?

Solution

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

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";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top