A Scala method to run any block of code slowly

The book, Advanced Scala with Cats, has a nice little function you can use to run a block of code “slowly”:

def slowly[A](body: => A) = try body finally Thread.sleep(100)

I’d never seen a try/finally block written like that (without a catch clause), so it was something new for the brain.

In the book they run a factorial method slowly, like this:

slowly(factorial(n - 1).map(_ * n))

FWIW, you can modify slowly to pass in the length of time to sleep, like this:

def slowly[A](body: => A, sleepTime: Long) = try body finally Thread.sleep(sleepTime)
Scala exception handling (try/catch/finally and more) alvin March 10, 2018 - 12:51pm

Scala FAQ: How do I handle exceptions in Scala?

Solution: Basic exception handling in Scala is handled with its try/catch/finally syntax. (See the end of this article for links to tutorials on advanced exception handling in Scala.)

Here’s an example of Scala’s try/catch/finally syntax:

Functional error handling in Scala alvin February 13, 2017 - 11:08am

Here are a few nice blog posts on functional error-handling in Scala:

- Easing into functional error handling in Scala
- How do I error handle thee?
- Designing fail-fast error handling

Scala: How to declare a variable (var) before using it in try/catch/finally

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.17, “How to declare a variable (var) before using it in try/catch/finally.”


You want to use an object in a try block, and need to access it in the finally portion of the block, such as when you need to call a close method on an object.

How to match one or more exceptions with try/catch in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.16, “How to match one or more exceptions with try/catch in Scala.”


You want to catch one or more exceptions in a try/catch block.


The Scala try/catch/finally syntax is similar to Java, but it uses the match expression approach in the catch block:

Scala control structure examples (if/then, match/case, for, while, try/catch)

This post contains a collection of Scala control structures examples. I initially created most of these in the process of writing the Scala Cookbook. Unlike the Cookbook, I don’t describe them much here, I just show the examples, mostly as a reference for myself (and anyone else that can benefit from them).

if/then control structures:

Here are some examples of the Scala if/then control structure:

Handling Drupal SQL exceptions (db_insert, db_update, db_delete)

While developing a Drupal module, I just ran into a situation where it may be common for users to generate SQL exceptions. I have a 'unique' limit on several of my database tables, essentially saying that certain name fields must be unique for the current project. As you can imagine, it's extremely easy to enter a duplicate name, and while doing a SQL INSERT that can easily lead to a SQL exception.

As a result, I dug around and found a way to handle SQL exceptions in Drupal queries. My current problem is with a Drupal 7 db_insert query, and I handled it like this: