1

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?

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.