Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About exception function!

 
dean lin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All

Please help me to modify this code let it can work.

The question is if I input one number not exceed 10000 it will caculate Math.pow(X,3)

If the number exceed 10000 it shows up ("Your input can not exceed 10000");

I want to understand what is the exception function and how it work.

Thank you very much.

 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exceptions so only be used when your application encounters an exceptional circumstance that it can not over come. There is much debut upon the subject of what an exceptional circumstance is.
It would be worth your while to look up Exceptions on the Sun Java site, it has an excellent explanation.

Now, that being said, are you sure that for your application an exception is the correct way to report what is basicly a validation (range check) mismatch?

G
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have several spelling errors in the code, eg cube/Cube.
You cannot have any code in a method after its return statement.
Your CalculateException class ought to extend RuntimeException.
Don't call the same method from inside itself. Use double xxx = [something]; then use xxx rather than cube();
The cube method should be labelled static, and there is no need to create the new Calculate object.
Call cube(123.45) not cube().

Correct those errors and then see how it works. Also sort out your indentation; the code is difficult to read in its present state.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your CalculateException class ought to extend RuntimeException.

You would like to make it an unchecked exception ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it would cause too much confusion to say "extends IllegalArgumentException". Making it a checked Exception would require a throws, and the original poster is having enough difficulty as it is.

And I think an IllegalArgumentException is an appropriate way to enforce a precondition, as in this constructor.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it would cause too much confusion to say "extends IllegalArgumentException".

I was thinking of "extends Exception". The poster already has the throws clause on his cube() method, and the catch clause in the main() method. I don't see where the confusion would be
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn't noticed there already is a throws declaration (sorry), in which case, yes, extends Exception would work all right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic