开发者

Why NumberFormatException is runtime?

开发者 https://www.devze.com 2023-03-31 04:39 出处:网络
Runtime exceptions indicate broken contract (like NPE) and should never be thrown if code has no errors. It always indicates error in code (same as asserts but asserts are for internal class errors wh

Runtime exceptions indicate broken contract (like NPE) and should never be thrown if code has no errors. It always indicates error in code (same as asserts but asserts are for internal class errors while Runtime are for class's client errors).

Runtime exceptions should never be catched.

Checked exceptions, on the other hand, are part of signature and should be catched and processed. They may indicate user input errors or external resource troubles (like IOException).

With all of it I 开发者_高级运维can't get why NumberFormatException is runtime?


Firstly, whoever told you

Runtime exceptions should never be caught

doesn't know much about Java. Don't listen to them - they are wrong.

NumberFormatException being a runtime exception: Unchecked exceptions are chosen because they indicate a programming error. It is possible to know before calling Integer.parseInt() (for example) that a String is a valid integer number, e.g. here's just one way:

if (str.matches("^\\d{1,8}$") {
    int myInt = Integer.parseInt(str); // will never throw NumberFormatException 
}

Therefore, it can be considered a programming error to ever get one - the programmer chose to not check first.

If you are not confident about the integrity/quality of the String you are about to parse, it's easy to catch:

try {
    // parse your string
} catch (NumberFormatException e) {
    // do something about it
}

The other reason to make it a runtime is that it doesn't clutter the code with potentially unnecessary try/catch blocks, if you are confident that you won't get one, e.g. if to totally trust the source of the String data.


NumberFormatException could also be thrown when parsing configuration files, in which case it would be a programmer error. When parsing user input you are usually using NumberFormat which throws a checked ParseException.


NumberFormatException extends IllegalArgumentException. The reason why this is a runtime exception is that it is completely possible to break the contract of a method that takes a String and returns a Number. If I pass in 123D and there is not a proper validation of data than this would be an appropriate illegal argument.


Why is NumberFormatException a runtime error? Well if you have a dialog where the user enters a value, and the value is not a number but is parsed as such then you would want to know about that. Is an exception the best way? Perhaps not, but it is what it is.


In a sense, NumberFormatException is a compile-time exception. But instead of being thrown by the Java compiler, it's thrown by the format string parser/compiler when your program runs it. The same applies to Pattern and other uses of regular expressions; your program is running the parser/compiler.


I think runtime exceptions happen when the hardware of the computer does an operation, then realizes that it's not possible, so it's too late to go back and throw an exception, so the program crashes. for number format exception, the computer tries to read, for example, a string as an int. The hardware tries to store a char in an int variable , which is not possible. basically anything that involves hardware, like division over 0

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号