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 6805119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:32:53+00:00 2026-05-26T19:32:53+00:00

I need to initialise a set of vals, where the code to initialise them

  • 0

I need to initialise a set of vals, where the code to initialise them might throw an exception. I’d love to write:

try {
  val x = ... generate x value ...
  val y = ... generate y value ...
} catch { ... exception handling ... }

... use x and y ...

But this (obviously) doesn’t work because x and y aren’t in scope outside of the try.

It’s easy to solve the problem by using mutable variables:

var x: Whatever = _
var y: Whatever = _
try {
  x = ... generate x value ...
  y = ... generate y value ...
} catch { ... exception handling ... }

... use x and y ...

But that’s not exactly very nice.

It’s also easy to solve the problem by duplicating the exception handling:

val x = try { ... generate x value ... } catch { ... exception handling ... }
val y = try { ... generate y value ... } catch { ... exception handling ... }

... use x and y ...

But that involves duplicating the exception handling.

There must be a “nice” way, but it’s eluding me.

  • 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-26T19:32:54+00:00Added an answer on May 26, 2026 at 7:32 pm

    A straightforward solution would be to define a wrapper function that uses by-name parameters.

    def safely[T](f: => T): T = try { f } catch { /* exception handling */ }
    // You'll have to play around with safely's type signature, depending on what
    // you do in the exception handling
    val x = safely { generateX }
    val y = safely { generateY }
    

    Or, if you’re feeling really swanky, Scala 2.9 allows a partial function to be used as the exception handler.

    val handler: PartialFunction[Throwable, Unit] = { /* exception handling */ }
    val x = try { generateX } catch handler
    val y = try { generateY } catch handler
    

    On the whole, I would say that the first method is the most straightforward. But if you can write reusable bits of exception handlers that can be composed together using orElse or andThen, then the second method would be more appropriate.

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

Sidebar

Related Questions

I need to initialise images from raw data downloaded from a server which delivers
I need to initialize a set of static String values stored in an XML
I need to be able to set a Data Source for both Access and
I need to create a function to create a set of integers. As I
I need to set/initialize an array exactly to what JSON will look like so
I need to initialize few objects, it can take a while so I want
How do I go about if I need to initialize an object's base with
I'm writing a web application and need to initialize some parameters that I'm pulling
I need a 'good' way to initialize the pseudo-random number generator in C++. I've
To initialize an int array with all zeros, do I need to use: int

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.