scala's Try elegant on error behavior

refactoring, scala, try-catch

Solution

I think your if statement is perfectly valid. Here is another alternative:

def doTheDangerousThing(): Try[Result] = Try(dangerousOp) recoverWith {
    case exception => println("error"); Failure(exception)
}

Problem

Is there a more elegant way of doing this in scala? ``` def doTheDangerousThing(): Try[Result] = { val result = Try(dangerousOp) if (result.isFailure) { println("error") } result } ```

Original source