【发布时间】:2014-03-28 12:54:17
【问题描述】:
我正在研究一种从双链接双端队列中获取元素的方法。当linkedlist 为空时,我得到一个nullpointerexception,我试图弄清楚如何处理它。我正在使用以下代码,但它仍然需要我返回 A。有什么想法可以进行此编译吗?
def peekBack():A = {
try {
last.data // return if the list is full if not, catch the nullpointerexception
} catch {
case ex: NullPointerException => {
println("There are no elements in this list.")
//Error is here, it is requiring me to return something!!!
}
}
}
谢谢!
【问题讨论】:
-
从 catch 块中返回 null
-
我试过了,但它给了我一个类型不匹配
-
是原始类型的双端队列中的数据还是对象?
-
类 LinkedDeQue[A] 扩展 Deque[A]
标签: java scala error-handling nullpointerexception try-catch