20

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Seth
  • 1,064
  • 11
  • 18
  • I'm not sure I would use it .... but you can use *any* (conforming) PartialFunction ... have unified error handling elsewhere? (Say `ignoreTheseSillyClosableExceptions`.) Besides, "it's just cool" that Scala can express it like that ;-) –  Oct 17 '11 at 02:42
  • For inspiration: Paul Phillips provided a motivational example when he implemented this: http://www.scala-lang.org/node/8070 – Arjan Blokzijl Oct 17 '11 at 08:18
  • 3
    The question would be better with an example or at least a link. – ziggystar Oct 17 '11 at 08:20
  • @ziggystar added a link to the patch notes – Seth Nov 24 '11 at 00:29

2 Answers2

25

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
axel22
  • 32,045
  • 9
  • 125
  • 137
7

Good answer by axel22, but I think the real reason for its introduction is something else. The try/catch/finally handling introduced a special case. You used a partial function literal, but you could not actually replace that with a partial function. Now, catch just receive a partial function, and one more special case in the language is gone.

Community
  • 1
  • 1
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • This actually confirms my initial reasoning - that this is mainly a "tidyness" feature. – Seth Nov 24 '11 at 00:26
  • Agreed - it's the uniformity principle at its best - a generalized construct having usability as a consequence. – axel22 May 25 '12 at 13:41