0

Need a bit quick help with Kotlin. Here is the pseudocode: When there is a “specialCase” error, throw the exception and Log this exception as “Info”. This is the if statement I have. Does this look like a good approach?

if (error.contains (specialCase)) 
{
    throw specialCaseDoesNotExistException
}
    LOGGER.info("WriteSpecialCaseasInfoandNOTError")
Trehman
  • 1
  • 3

1 Answers1

0

Either you log before throwing or you could combine it into a single statement using also (reference):

if (error.contains (specialCase)) {
    LOGGER.info("WriteSpecialCaseasInfoandNOTError")
    throw specialCaseDoesNotExistException
}
if (error.contains (specialCase)) {
    throw specialCaseDoesNotExistException.also {
        LOGGER.info("WriteSpecialCaseasInfoandNOTError")
    }
}
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
  • hey @s1m0nw1, appreciate the input. Your suggestions make sense but does either of those 2 options is tied to this --> https://stackoverflow.com/questions/6639963/why-is-log-and-throw-considered-an-anti-pattern/6640029#6640029 – Trehman Jan 19 '21 at 19:01