加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java如果返回NoSuchElementException

发布时间:2020-12-15 04:28:55 所属栏目:Java 来源:网络整理
导读:如何创建if语句来检查函数是否返回“NoSuchElementException”?类似于我下面的东西. if (functionReturns == "NoSuchElementException") 解决方法 How can I make an if statement to check if NoSuchElementException” is returned from a function? 如果
如何创建if语句来检查函数是否返回“NoSuchElementException”?类似于我下面的东西.

if (functionReturns == "NoSuchElementException")

解决方法

How can I make an if statement to check if NoSuchElementException” is
returned from a function?

如果您的意思是您的函数返回值为NoSuchElementException的String,请使用equals而不是==:

if("NoSuchElementException".equals(functionReturns)) { }

如果您的意思是您的函数可以抛出NoSuchElementException,请使用try-catch.当函数抛出NoSuchElementException时,将触发catch块.

try {
    function();
} catch(NoSuchElementException e) {
     //NoSuchElementException was thrown

}

如果您的意思是您的函数实际上返回NoSuchElementException的实例,您可以使用:

NoSuchElementException.class.isAssignableFrom(functionReturns)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读