2

Doing some functional programming in Scala, and coming from a Java background, I tend to throw my exceptions like this:

throw new MyException("something is wrong")

However, I've also seen people omit new:

throw MyException("something is wrong")

Which is the more idiomatic way?

Jamy Mahabier
  • 400
  • 5
  • 16
  • 3
    It doesn't matter. If you're trying to follow best function programming/Scala practices, you should be striving to not throw exceptions, anyway. – Michael Zajac Mar 10 '17 at 14:01
  • If the Exception class is an object, then you may omit it. Otherwise, you're calling the class – OneCricketeer Mar 10 '17 at 14:04

1 Answers1

6

I'd say the idiomatic way is to avoid exceptions altogether and use Either instead. Scala is a functional language. Functions should return values, not have side effects. Exceptions are side effects. Either gives you the means to define a happy path and a sad path, separately.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588