如何检查或投掷用户定义的异常,以了解枚举中不存在的值,请说我通过元数据=“ DF”来自邮递员
public enum Metadata{
AC, CD;
}
我在传递枚举中不存在的值时会遇到JSON解析器错误。该错误非常漫长且对用户不可读取。
public enum Metadata{
AC, CD;
}
I am getting json parser error when passing value which is not there in enum..and that error is very lengthy and not readable to user ..instead I should get a proper readable message but I dont know how to resolve this error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
,然后您需要实现一种可以验证输入的方法,如果提供了 enum-name ,则不存在,它将带有适当的消息的例外。
这样就是这样:
比捕获
IllegalArgumentException
(因为它是一个运行时异常),它可以更干净,可以抛出value> value> valueof()
,然后扔一个catch
块所需的例外。而且,这种方法将无法获得显着的性能优势,因为只有少数枚举会员。仅出于说明目的,这就是可以捕获和复发的方式:
Then you need to implement a method that would validate the input and in the case if provided enum-name doesn't exist it would throw an exception with a suitable message.
That's how it might look like:
It would be more cleaner approach than catching an
IllegalArgumentException
(because it's a runtime exception) that can be thrownvalueOf()
, and then throwing a desired exception from thecatch
block. And you can't gain a significant performance advantage with this approach since there will be only a few enum-members.Just for illustrative purposes, that's how an exception can be caught and rethrowing: