Java:当用户输入超出范围的数字时是否有异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28257570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Is there an exception for when a user enters a number out of range
提问by user3505177
Outside of the context of an Array index, is there any exception in Java that can be thrown when the user enters a number outside the range asked for by a program?
在 Array 索引的上下文之外,当用户输入程序要求的范围之外的数字时,Java 中是否有任何可以抛出的异常?
ie. The program wants an integer between 0-100 and user enters 132, could an exception be thrown manually or does it have to be custom?
IE。程序需要一个 0-100 之间的整数,用户输入 132,异常可以手动抛出还是必须是自定义的?
EDIT: I get that it doesn't make sense to use an exception for this, it's for an assignment and I want to know my options. Easy on the hair trigger down voters.
编辑:我知道为此使用异常是没有意义的,它用于作业,我想知道我的选择。容易在头发上触发选民。
回答by shimonb89
You can throw IllegalArgumentException
你可以抛出 IllegalArgumentException
回答by sprinter
If you particularly want to use exception handling for this then you should probably define your own custom exception for it rather than reuse a standard Java one. That way you can be certain you are only catching your particular case. If you do define an exception you will need to manually throw it and catch it.
如果您特别想为此使用异常处理,那么您可能应该为它定义自己的自定义异常,而不是重用标准的 Java 异常。这样你就可以确定你只是在捕捉你的特殊情况。如果您确实定义了异常,则需要手动抛出并捕获它。
Note that I am referring here specifically to an exception related to user entry that you wish to handle. Java has an excellent set of exceptions for handling errors that are internal to your code (e.g. null pointer exceptions).
请注意,我在这里特指与您希望处理的用户条目相关的异常。Java 有一组出色的异常用于处理代码内部的错误(例如空指针异常)。
Having said that this does not seem like a good use case for an exception. Your code should consider every value a user might enter as within normal use. Exception handling is best reserved for handling things that are exceptions to normal operations.
话虽如此,这似乎不是异常的好用例。您的代码应该考虑用户可能在正常使用范围内输入的每个值。异常处理最好保留用于处理正常操作的异常情况。
回答by MrSimpleMind
Java Language Specification, regarding Integer Operationssays:
The integer operators do not indicate overflow or underflow in any way.
整数运算符不以任何方式指示上溢或下溢。
An integer operator can throw an exception (§11) for the following reasons:
由于以下原因,整数运算符可能会引发异常(第 11 节):
Any integer operator can throw a
NullPointerException
if unboxing conversion (§5.1.8) of a null reference is required.The integer divide operator / (§15.17.2) and the integer remainder operator % (§15.17.3) can throw an
ArithmeticException
if the right-hand operand is zero.The increment and decrement operators ++ (§15.14.2, §15.15.1) and -- (§15.14.3, §15.15.2) can throw an
OutOfMemoryError
if boxing conversion (§5.1.7) is required and there is not sufficient memory available to perform the conversion.
NullPointerException
如果需要空引用的拆箱转换(第 5.1.8 节),则任何整数运算符都可以抛出 a 。ArithmeticException
如果右侧操作数为零,则整数除法运算符 /(第 15.17.2 节)和整数余数运算符 %(第 15.17.3 节)可以抛出 an 。递增和递减运算符 ++(第 15.14.2 节,第 15.15.1 节)和 --(第 15.14.3 节,第 15.15.2 节)可以抛出一个
OutOfMemoryError
if 装箱转换(第 5.1.7 节)是必需的,并且没有足够的内存可用于执行转换。
I m not a fan of "exception driven architecture", ;)
我不是“异常驱动架构”的粉丝,;)
Implement a check if the integer is over 100 and return an error, status code, etc as feedback for user/system etc.
执行检查整数是否超过 100 并返回错误、状态代码等作为用户/系统等的反馈。
回答by huseyin tugrul buyukisik
Exceptions are expensive. Should use some cheap test function or make a wrapper class that accepts only the range you want and carries an "error" variable through all calculations to the end. You check that error at end, trace it back to the source. Traveling error makes it possible to see multiple errors at the same time, not just stuck at the first error happened.
例外是昂贵的。应该使用一些廉价的测试函数或制作一个包装类,它只接受您想要的范围并在所有计算中携带一个“错误”变量直到最后。您最后检查该错误,将其追溯到源头。旅行错误使同时看到多个错误成为可能,而不仅仅是停留在发生的第一个错误上。
public class IntegerWithError
{
int result;
int error;
int errorSource;
// some methods to check for range, alter "error" as needed.
// some methods to sign the start point of error
}
private static IntegerWithError Test(int parameter)
{
IntegerWithError iwe=new IntegerWithError();
if(parameter>5)
{
iwe.error=1;
iwe.errorSource=iwe.hashCode();
}
iwe.result=parameter;
return iwe;
}
....
ParameterWithError p1,p2;
....
MatrixMultiplyWithError(p1,p2);
RenderWithErrors(p1,p2,b1,b1);
TraceError(p1); // finds if it was a function, a constructor or something.