Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6726531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:56:18+00:00 2026-05-26T09:56:18+00:00

I’ve read about and experimented with the Scala 2.9 try…catch feature, and it has

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:56:19+00:00Added an answer on May 26, 2026 at 9:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.