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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:30:23+00:00 2026-06-15T05:30:23+00:00

I know in Scala a method should never return null … but what’s about

  • 0

I know in Scala a method should never return null… but what’s about input parameters? Given the following code snippet…

object MyObject {

    def myMethod(p: String): Option[String] = {
        if (p == null) throw new IllegalArgumentException("p is null.")
        ...
    }
}

… is the way I check p correct? Is there any recommendation?

  • 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-15T05:30:27+00:00Added an answer on June 15, 2026 at 5:30 am

    The convention is that Scala code does not use nulls (with a tiny number of exceptions, which should immediately be fixed when using those library functions).

    Thus, a null from Scala is a sign that something has gone wrong (at least a PBCAK), so you may as well throw an exception. This is not routine operation; this is something seriously screwed up. Catch the exception wherever you catch serious screw-ups. Catching an IllegalArgumentException instead of a NullPointerException adds no extra information. Just leave the original alone.

    If the code comes from Java, the canonical way of dealing with it is to wrap it in Option, which will convert null to None. Then you probably don’t even need to throw an exception; just return a None.

    def myMethod(p: String) = Option(p).map(_.toLowerCase)
    

    If you cannot continue when it is null, then you need to consider whether an informative exception would help. Option(p).orElse(throw new IllegalArgumentException("Null!")) is one compact way of expressing the exception-throwing sentiment.

    In Scala 2.10, you can also wrap things in scala.util.Try(...) which will automatically catch and package the exception for you. If you want a packaged exception instead of a thrown one, this is the way to go. (And use Try instead of Option.)

    import scala.util.Try
    def myMethod(p: String) = Try(p.toLowerCase)
    

    Finally, for more general handling of alternative outcomes, use Either. The convention for error handling is that the expected output is a Right(whatever), while Left(whatever) indicates that something went wrong.

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

Sidebar

Related Questions

I would like to know if the following is possible in Scala (but I
(Yes I know I can call Java code from Scala; but that is pointless;
As far as I know, the infix operator usage in Scala should be equivalent
I meet some scala code with ? but do not know what it mean
I feel like I should know this but I've been stumped for hours now
Does anyone know how to get the (Scala part only) desugared translation of a
I'm new to Scala and don't know Java. I want to create a jar
I have played around with Scala for a while now, and I know that
Can someone please explain the major differences between Scala, Groovy and Clojure. I know
I would like to know if it is possible to abstract the copy method

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.