I have the following code to decode a BitVector. How can I remove the exception and throw a failure?
def decode(b: BitVector) = {
if (!applies) {
Attempt.successful(DecodeResult.apply(null, b))
} else {
string.decode(b) match {
case Attempt.Successful(str) => {
val contentSize = str.value.slice(0, 2)
val content = str.value.slice(2, contentSize.hex2int() + contentSize.length)
if(!content.forall(_.isLetterOrDigit)){
throw new IllegalArgumentException()
}
val remain = str.value.slice(contentSize.hex2int() + contentSize.length, str.value.length)
Attempt.successful(DecodeResult.apply(content, BitVector(remain.getBytes)))
}
case Attempt.Failure(e) => Attempt.failure(e)
}
}
}