Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the use cases for Scala 2.9's try...catch generalization?

Tags:

I've read about and experimented with the Scala 2.9 try...catch feature, and it has me thinking about possibilities. What would I actually use it for other than saving a couple of lines of code?

Scala 2.9 Final Release Notes

like image 288
Seth Avatar asked Oct 17 '11 02:10

Seth


People also ask

What is try catch in Scala?

Like Java, Scala has a try/catch/finally construct to let you catch and manage exceptions. The main difference is that for consistency, Scala uses the same syntax that match expressions use: case statements to match the different possible exceptions that can occur.

What is a try catch?

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception.


1 Answers

The use case is to be able to have generic error handling throughout your application. Let's say you want to handle all FileNotFoundExceptions in your application by sending an e-mail to an administrator. Previously, you'd have to do it like this:

// Globally
val fileNotFound: PartialFunction[Throwable, Unit] = {
  case e: FileNotFoundException =>
    // Create report and send the e-mail
}

// On each try-catch-block
try {
  // Open file
} 
catch {
  case fnf: FileNotFoundException => fileNotFound(fnf)
}

Now you just do:

try {
  // Open file
} catch fileNotFound

This also has the nice advantage that you can link several such exception handlers using the orElse method on partial functions:

val fileErrors = fileNotFound orElse endOfFile orElse invalidFormat

And then just use that everywhere where you need file exception handling. Such an error handler can be dynamically combined based on the configuration file for the application, for example. This is much less cumbersome than pattern matching everywhere and calling the correct handler.

One useful thing which could be pimped on top of partial functions is the andAlso operator, which acts as a sequencing operator on two partial functions. This would be useful when you want to do some error handling specific to a particular try-catch block after having done the generic error handling.

implicit def pf2ops(pf: PartialFunction[Throwable, Unit]) = new {
  def andAlso(localpf: PartialFunction[Throwable, Unit]) = new PartialFunction[Throwable, Unit] {
    def apply(t: Throwable) = {
      if (pf.isDefinedAt(t)) pf(t)
      localpf(t)
    }
    def isDefinedAt(t: Throwable) = pf.isDefinedAt(t) || localpf.isDefinedAt(t)
  }
}

And then you can do this:

scala> try {
     |   throw new java.io.FileNotFoundException
     | } catch fnf andAlso {
     |   case e: Exception => println("I don't know, but something is specific to this particular block.")
     | }
I don't know, but something is specific to this particular block.

I guess you could play further with the exact semantics and the meaning (and the name) of andAlso.

like image 192
axel22 Avatar answered Oct 02 '22 18:10

axel22