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

  • SEARCH
  • Home
  • 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 7181123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:29:29+00:00 2026-05-28T17:29:29+00:00

What is the best way (concisest, clearest, idiomatic) to catch a MatchError , when

  • 0

What is the best way (concisest, clearest, idiomatic) to catch a MatchError, when assigning values with pattern matching?

Example:

val a :: b :: Nil = List(1,2,3) // throws scala.MatchError

The best way I found so far:

val a :: b :: Nil = try {
    val a1 :: b1 :: Nil = List(1,2,3)
    List(a1, b1)
  catch { case e:MatchError => // handle error here }

Is there an idiomatic way to do this?

  • 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-28T17:29:30+00:00Added an answer on May 28, 2026 at 5:29 pm

    Slightly improving on Kim’s solution:

    val a :: b :: Nil = List(1, 2, 3) match {
      case x @ _ :: _ :: Nil => x
      case _ => //handle error
    }
    

    If you could provide more information on how you might handle the error, we could provide you a better solution.

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

Sidebar

Related Questions

the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
What is the best way of creating an alphabetically sorted list in Python?
What is the best way to iterate through a strongly-typed generic List in C#.NET
Best way to illustrate my question is with this example code: class Item {}
What is the best way to implement the following example ? Actor server receives
What is the best way to update a value in a list of tuples?
Best way to elaborate on the question is to give an example of what
The best way of describing this is I have a table of people with
The best way to store images into MySQL is by storing the image location

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.