Scala - the life saver

Scala is very complex, and yet the features I love the most are among the simplest pleasure it provides.

Consider, for example, how would you express this in Java:

def errorClause(message: String): PartialFunction[Throwable, Unit] = {
  case NonFatal(e) => {
    log.error(message, e.getMessage())
    Notification.show(message, Notification.Type.ERROR_MESSAGE)
  }
}

It can be used like this:

try {
  // some code
} catch uiController.errorClause("Operation unsuccessful")

It is very simple, is not it? I need this code to interact with Java code in the situations where I do not really expect an error and could do nothing if it appears. Can you imagine amount of Java code I would need to write every time I do such an operation?

Posted On

Category:

Tags: / /