I wrote a function that takes an array of integers. If the function has at least two integers, it will flip the first two integers and return a new array. (This is a problem in Scala For The Impatient, which I am working through for fun.) If the array has less than two integers, it throws an exception.
Should I be using an Exception besides Exception? I usually program in Python, where just using the Exception class is frowned up. (Though I have seen plenty of people use it.) I do know that Scala has a MatchError class, but I'm not sure if that's the one I should use.
def swap2(arrayOfInts: Array[Int]): Array[Int] =
arrayOfInts match {
case Array[Int](a, b, rest*) => Array.concat(Array(b, a), rest.toArray)
case Array[Int](a) => throw Exception("Array needs at least two integers.")
}