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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:40:38+00:00 2026-05-27T16:40:38+00:00

I am writing a web app where exceptions are used to handle error cases.

  • 0

I am writing a web app where exceptions are used to handle error cases. Often, I find myself writing helpers like this:

def someHelper(...) : Boolean {...}

and then using it like this:

if (!someHelper(...)){
    throw new SomeException()
}

These exceptions represent things like invalid parameters, and when handled they send a useful error message to the user, eg

try {
     ...
} catch {
    case e: SomeException => "Bad user!"
}

Is this a reasonable approach? And how could I pass the exception into the helper function and have it thrown there? I have had trouble constructing a type for such a function.

  • 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-27T16:40:39+00:00Added an answer on May 27, 2026 at 4:40 pm

    I use Either most of the time, not exceptions. I generally use exceptions, as you have done or some similar way, when the control flow has to go way, way back to some distant point, and otherwise there’s nothing sensible to do. However, when the exceptions can be handled fairly locally, I will instead

    def myMethod(...): Either[String,ValidatedInputForm] = {
      ...
      if (!someHelper(...)) Left("Agree button not checked")
      else Right(whateverForm)
    }
    

    and then when I call this method, I can

    myMethod(blah).fold({ err =>
      doSomething(err)
      saneReturnValue
    }, { form =>
      foo(form)
      form.usefulField
    })
    

    or match on Left(err) vs Right(form), or various other things.

    If I don’t want to handle the error right there, but instead want to process the return value, I

    myMethod(blah).right.map{ form =>
      foo(form)
      bar(form)
    }
    

    and I’ll get an Either with the error message unchanged as a Left, if it was an error message, or with the result of { foo(form); bar(form) } as a Right if it was okay. You can also chain your error processing using flatMap, e.g. if you wanted to perform an additional check on so-far-correct values and reject some of them, you could

    myMethod(blah).right.flatMap{ form =>
      if (!checkSomething(form)) Left("Something didn't check out.")
      else Right(form)
    }
    

    It’s this sort of processing that makes using Either more convenient (and usually better-performing, if exceptions are common) than exceptions, which is why I use them.

    (In fact, in very many cases I don’t care why something went wrong, only that it went wrong, in which case I just use an Option.)

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

Sidebar

Related Questions

I am writing a simple ASP.NET MVC web app. At this point, I am
I'm writing this Web app that is associated with a Microsoft sql database. I
I'm writing a web app, and I'd like to use repoze.what & repoze.who to
I was writing a web app for Android which can be used in both
I'm writing a web app that points to external links. I'm looking to create
I am writing a web app using TurboGears, and in that app the users
I'm writing a web app (Java) which allows users to select contacts. The contacts
I am writing a web app in PHP, and it has come time to
I'm writing a web app for the iPad ( not a regular App Store
i'm totally noob to asp.net mvc and currently i'm writing an web app which

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.