scala - 如何在 scodec Attempt 中引发错误?
问题描述
我有以下代码来解码 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)
}
}
}
解决方案
推荐阅读
- c - N>47 的斐波那契数生成器中断
- html - 在 Seaside 中使用图像作为可点击项创建超链接
- c - 使用 fscanf 时 C 中的分段错误(核心转储)
- python - 将升力、支持度和信心四舍五入到小数点后 4 位,但升力变为小数点后 1 位
- r - 在我的列的每一行中划分最后一行
- python - Python:如果列表长于1,则对列表的元素求和
- java - 如何在 gradle 启动测试中关闭关闭挂钩的输出?
- javascript - cannon.js 对象正在端到端翻转
- c++ - CMake相对链接,可执行文件找不到共享库
- vb.net - 在 vb.net 中处理对象和垃圾收集