2

我有以下代码来解码 BitVector。如何删除异常并引发失败?

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)
    }
  }
}
4

0 回答 0