返回一个值并抛出异常? [英] Return a value AND throw an exception?

查看:501
本文介绍了返回一个值并抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用声称如果成功则返回true的API,如果失败则返回false。但是,如果它失败,它还声称会抛出不同的例外。它如何返回false并抛出异常?

I'm working with an API that claims to return true if it succeeds, and false if it fails. But, it also claims to throw different exceptions if it fails. How can it return false and throw an exception?

推荐答案

既不能抛出异常又从单个返回值功能调用。

It's not possible to both throw an exception and return a value from a single function call.

如果出现错误,可能会返回 false ,但如果输入无效则抛出异常。

Perhaps it does something like returning false if there's an error, but throwing an exception if the input is invalid.

编辑: PaulPRO 发布了(现已删除)答案指出在技术上可能导致异常被抛出到另一个线程中,同时返回当前值中的值。我认为这值得注意,即使它不是你应该看到的东西。

edit: PaulPRO posted a (now-deleted) answer pointing out that it is technically possible to cause an exception to be thrown in a different thread, while returning a value in the current one. I thought this was worth noting, even if it's not something you should ever see.

这篇关于返回一个值并抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆