这里哪个例外最合适?
我正在实用程序方法中验证来自 Restlet URL 的一些输入(这样,如果我采取的行为发生变化,我只更改一个地方,是的)。 legalName 本质上验证该值是否为字母数字,但我可能很快就会允许使用其他字符。
我尝试让我的异常有意义 - 在这种情况下什么异常是最好的?
public static String getProperty(Request request, String key) {
String value = request.getAttributes().get(key).toString();
// unless something is specifically text, it is a property
if(legalName(value)) return value;
throw new IllegalArgumentException(value);
}
我的想法是:
- IllegalArgumentException - 密钥直接导致无效结果
- IllegalStateException - 我们试图使用不合格的值
- 无例外 - 返回一个空字符串并记录发生违规的事实
- 无例外 - 删除任何不需要的字符,返回经过清理的字符串,并记录事实
当然我不是第一个必须在之前验证输入的人:-)
I am validating some input from a Restlet URL in a utility method (that way if the behavior I take changes, I only change it one place, yay). The legalName essentially validates the value as being alphaNumeric, but I may allow other characters soon.
I try to keep my Exceptions to make sense - what Exception would be the best in this scenario?
public static String getProperty(Request request, String key) {
String value = request.getAttributes().get(key).toString();
// unless something is specifically text, it is a property
if(legalName(value)) return value;
throw new IllegalArgumentException(value);
}
My thoughts are:
- IllegalArgumentException - the key directly leads to an invalid result
- IllegalStateException - we're trying to use a non-conforming value
- No exception - return an empty string and log the fact that a breach was made
- No exception - remove any undesireable characters, return the sanitized string, and log the fact
Surely I'm not the first person to have to validate input before :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的异常应该在存储属性时抛出,而不是检索。检查 Restlet 验证 。
如果您更愿意坚持您的解决方案:
validateName(..)
并让它抛出异常。如果没有 - 返回该值。javax.validation.ValidationException
看起来是更好的选择,但您可以使用您提到的两个IllegalXException
中的任何一个。只要确保他们有更详细的消息即可。Exceptions like this should be thrown when the property is stored, not retrieved. Check Restlet validation for that.
If you prefer to stick to your solution:
validateName(..)
and let it throw the exception. If it doesn't - return the value.javax.validation.ValidationException
looks the better option, but you can use any of the twoIllegalXException
you mentioned. Just make sure they have a more detailed message.这似乎是无效的用户输入,因此首先我会尝试某种 ValidationException 并使用验证框架,而不是自己管理所有内容。如果不是 ValidationException,那么 IllegalArgumentException 可能是您列出的选择中最有意义的。
That appears to be invalid user input, so first I'd go for a ValidationException of some kind and use a validation framework instead of managing it all yourself. If not the ValidationException, then IllegalArgumentException probably makes the most sense out of your listed choices.
您可以使用自定义(您自己的)异常类。根据情况或验证,您可以使用适当的消息抛出它。
例如: 1.对于非法参数异常情况,在 try catch 中处理该代码块,并在 catch 中抛出您自己的带有正确消息的异常。
You can make use of Custom(Your Own) Exception Class .Depending upon the situation or validation you can throw it with proper message.
For Ex: 1.For Illegal Argument Exception case Handle that block of code in try catch and in catch throw your own Exception with Proper message.