• Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom Exceptions: is there a more concise way to demonstrate the concept?  RSS feed

 
John Pacuta
Greenhorn
Posts: 26
1
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I am learning about exceptions and I made a simple example after reading some lecture slides here. This is my example of the concept:



My question is: Is this the simplest and correct way to demonstrate the concept of custom exceptions? Could it be simpler? Is it incorrect?
Thank you for being part of the Java Community.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Bee wrote:. . . this code snippet is a somewhat simpler example...
. . .
Not that you should ever throw and catch an Exception in the same place in real life. All you achieve is a very expensive version of if‑else.
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
John Pacuta
Greenhorn
Posts: 26
1
Android Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!