John Pacuta wrote:. . . Could it be simpler? Is it incorrect? . . .
I am afraid the answer to both is yes.
Don't say throws Exception when you know are throwing a specific Exception. Don't call a class e. Call it SomethingException. That is not a hello world app so don't call it HelloWorld. The constructor is wrongly written. A constructor should set up all the fields so the object is in a consistent state. Your constructor doesn't do that; it simply discards the message.
It used to be possible to extend an Exception class simply by writing four constructors but many Exceptions now have more constructors. In Java5 you would have written… and that was it. I presume you are familiar with super(); Sometimes that would not compile because the superclass didn't have all four constructors, in which case you could change it to this:-In that case the hardest part is deciding which Exception to choose as your superclass. Probably a bad idea in that instance to choose plain simple Exception because it is “checked” and that means you must handle the Exception. Maybe you could have NegativeInputException extends IllegalArgumentException, which would be easier to write because it is not “checked”.
Also your text (where did you find that?) with Math.abs is incorrect. That will not reliably pick up negative numbers. What is wrong with < 0 which will reliably pick up negative numbers? Or <= 0?
John Pacuta wrote:My question is: Is this the simplest and correct way to demonstrate the concept of custom exceptions? Could it be simpler? Is it incorrect?
Ditto to everything Campbell wrote as far as correctness. I would start by implementing his advice.
Then, once you've changed the structure of your exception class e into SomethingException (or whatever you want to call it), this code snippet is a somewhat simpler example...
It's a trivial (i.e. uninteresting) example. For the purposes of demonstrating how exceptions work, it allows you to create an example without creating the function readInt() or instantiating a new instance of HelloWorld().
One more thing. Campbell already mentioned that this check should have been done with a < or <= operator.
There is something more. Your test will fail for one corner-case negative value.
It is because of how two's-complement integer values work.
Can you figure out what value it is?
Hint: The javadoc of Math.abs(int) mentions this value.
Thank you for your replies. In response to your questions:
Campbell Ritchie wrote:Also your text (where did you find that?) with Math.abs is incorrect
I made it myself when I constructed my novice example. I try to discover things about code by writing it, but it can leave me recovering my ego haha!
Paweł Baczyński wrote: Can you figure out what value it is?
I remember being quizzed about this in Enthuware. I looked it up in the Java API and the corner-case is: "If the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative." Which means to me that for the value Integer.MIN_VALUE the test will be false (where it should be true by how I was thinking). Therefore my logic fails, and to test for a negative integer, the correct test it:
See you around the ranch!
Aspiring Java Programmer.
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it: