问题
I came across below doubts while learning java exception handling,
Most(nearly all) of the example i've seen thows an exception using try bock and the appropriate catch prints a message to the user in the console and finally block used to close/release resources.
- Is exception handling is only about printing the message/cause for exception?
- While learning arithmetic exception the only example i got is DividebyZero scenario, is there any other operation that can cause arithmetic exception?
回答1:
No, exception handling is for handling, well, exceptional circumstances. I'd imagine that the examples you see aren't complete. Logging/printing the stack trace to System.err
could be a good idea, but really isn't always necessary (although invaluable when trying to debug your code), and that is certainly not all exception handling is used for.
For example, say you have a program that parses input that the user put into a text box:
int userValue = Integer.parseInt(textBox.getText());
What happens if the user inputs a letter? Well, that's an exceptional condition -- parseInt()
is trying to parse an integer, so what in the world is it going to do with a letter? Thus, the exceptional condition triggers a NumberFormatException
. Here, the proper way of dealing with the exception would probably be to alert the user that his/her input was invalid. No messing with the stack trace needed.
As for question 2, you can get ArithmeticExceptions
when trying to produce a BigDecimal
whose decimal representation doesn't end:
BigDecimal a = new BigDecimal("1").divide(new BigDecimal("3"));
If memory serves, that should throw an ArithmeticException
...
回答2:
No, exception handling isn't about printing a message or a cause for exception, although those are essential to figuring out why the exception was thrown in the first place.
Exception handling is really more geared to us acknowledging that weird things can happen during the lifecycle of a program, and depending on the type of "weird thing" that comes up during a run, we can hope to recover from it.
Take, for example, your ArithmeticException
. This is an exception which occurs when some part of the application decides to divide something by zero, or for other reasons (other classes may throw this; you'd have to read the documentation and figure out why). Java decides that it can't reasonably recover from that, so it throws a runtime exception, or an exception that isn't typically meant to be caught.
I encourage you to look at the Exceptions Trail from Oracle, as that will provide invaluable knowledge as to what the different types of exceptions are. Just note that, if you do have an exception in your code, it is better to investigate the cause of it, rather than catching it (and/or suppressing it).
回答3:
Exception handling in Java is about error management. It's a way to gracefully handle errors in your programs. For example, you can use Integer.parseInt() to convert a String to an integer. It throws a NumberFormatException if it cannot parse the String. In that case, you can catch the exception, default the value, and display an appropriate message to the user. And your program will be more robust and elegant.
回答4:
There are two types of reasons which lead to fail our program. One is an error and the other one is an exception. Errors are occurring due to the inabilities of the systems (Memory issues). However Exceptions are a type of issues which are occurring due to the problems in the written code.Hence those can be easily managed by the programmers. That is why as programmers we are supposed to have a knowledge of exception handling, in order to mitigate the program failures.
来源:https://stackoverflow.com/questions/23796319/purpose-of-java-exception-handling