gpt4 book ai didi

java - 哪个异常最适合 "you shouldn' t 在此上下文“场景中运行此方法?

转载 作者:行者123 更新时间:2023-11-29 07:44:33 25 4
gpt4 key购买 nike

在接口(interface)中,我有一个方法可以在一个实现中使用,但不应该在另一个实现中使用。如果有人在那个特定的实现中使用那个方法,我正在考虑抛出一个异常。 (这并不是说很容易意外遇到该异常)

更具体地说,该方法是getPlayer(String),它通过名称获取一个PlayerPlayer 是连接到服务器的客户端,但是在我的第二个实现中,不可能获得任何连接的客户端,因为第二个实现代表存储在在某处列出。

感谢任何帮助。让我知道代码粘贴是否能更好地帮助描述场景的性质。

最佳答案

我会抛出 IllegalStateException

Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

或者如果您的实现根本不支持它,您也可以使用 UnsupportedOperationException

Thrown to indicate that the requested operation is not supported.

关于java - 哪个异常最适合 "you shouldn' t 在此上下文“场景中运行此方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27061324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com