John Smith
John Smith

Reputation: 781

Convert F# failwith to Scala

How is it possible to convert a F# failwith exception to Scala. I have the following code:

 let rec getAny text = 
    let FailureText = "Text."
    if test then failwith FailureText
    else text

Just throw an Exception like the following?

throw new IllegalArgumentException("Text.");

In F# it would throw an Microsoft.FSharp.Core.FailureException. What would you throw in Scala? Just a normal java.lang.Exception or RuntimeException?

Upvotes: 1

Views: 367

Answers (1)

Didier Dupont
Didier Dupont

Reputation: 29528

You may use sys.error(failureText), it just throws a RuntimeException. Otherwise, throw is fine if you want to choose another exception, or if error does not convey the intent.

You may also consider assert, assume or require (defined in Predef)

And of course you may dispense with the else, but this is a matter of taste.

Upvotes: 4

Related Questions