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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:04:27+00:00 2026-06-14T04:04:27+00:00

Try/catches can be used like an expression, so: scala> try { 3 } catch

  • 0

Try/catches can be used like an expression, so:

 scala> try { 3 } catch {case _ => 0}
 res52: Int = 3

Also:

scala> try { 3 } catch {case _ => 0} finally try {println("side effect")} catch { case _ => println("catching side effect") } 
side effect
res50: Int = 3

so why not:

 scala> try { 3 } catch {case _ => 0} + 4
 <console>:1: error: ';' expected but identifier found.
   try { 3 } catch {case _ => 0} + 4

or why not:

scala> try { 3 } catch {case _ => 0} match {case 3 => "hi"}
<console>:1: error: ';' expected but 'match' found.
   try { 3 } catch {case _ => 0} match {case 3 => "hi"}

My goal is a function definition like this:

def transact[T](code : Unit => T):T = 
   try {startTransaction; Right(code)} 
     catch {case t:Throwable => Left(t)} 
   finally try {endTransaction} 
     catch { case e:... if ... => throw e}  
 match  {
   case Right(e) => e
   case Left....
 }

Of course I could just store the try/catch in a val and match the val:

 def transact[T](code : Unit => T):T = 
 {
   val transa = try {startTransaction; Right(code)} 
     catch {case t:Throwable => Left(t)} 
   finally try {endTransaction} 
     catch { case e:... if ... => throw e}  
transa match { 
 case ...
}
}

but then it no longer is a single expression, and I need to wrap in another {} which – please corect me if I’m wrong- means another layer of function object wrapping aka indirection, at a performance critical place.

So, is there a way to use try as a full expression and avoid this indirection?

thanks

  • 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-06-14T04:04:29+00:00Added an answer on June 14, 2026 at 4:04 am

    It’s a scala grammar issue — just add parenthesis to turn the try/catch block into a SimpleExpr so you can continue operating on it:

    scala> (try { 3 } catch {case _ => 0}) + 4
    res1: Int = 7
    
    scala> (try { 3 } catch {case _ => 0}) match {case 3 => "hi"}
    res2: java.lang.String = hi
    

    As usual, curly and round brackets are (mostly) interchangeable — your target code will look nicer (imo) with curly brackets.

    No idea about indirections — you would need to look at the compiled bytecode for that — but I doubt it will make a difference.

    For a full explanation on the grammar issue (which is not exactly an operator precedence issue as I initially believed), see: https://stackoverflow.com/a/7530565/178551

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

Sidebar

Related Questions

I am currently using two catch blocks after a try block. The first catches
I am nesting try catches inside of a main try catch statement, what I
try/catch can not handle errors in asynchronous functions. Of course, it is possible to
Suppose I have something like this : try code_that_fails() catch _:_ -> ..... How
How can I write a try / except block that catches all exceptions?
I'm wondering if I can put a try catch about $application->bootstrap()->run(); that will catch
I usually layout my Java SE methods like this. Should I try to catch
In C# you can do this: try { // some code here } catch
If I have some code inside a big try catch which eventually catches an
I understand how try-catch works and how try-finally works, but I find myself using

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.