且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

返回一个值并抛出异常?

更新时间:2023-12-04 23:08:34

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

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.