Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes About exception function! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About exception function!" Watch "About exception function!" New topic
Author

About exception function!

dean lin
Greenhorn

Joined: May 11, 2009
Posts: 3
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

Joined: Jan 01, 2007
Posts: 333

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

Joined: Oct 13, 2005
Posts: 39053
    
  23
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Your CalculateException class ought to extend RuntimeException.

You would like to make it an unchecked exception ?


[My Blog]
All roads lead to JavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Oct 13, 2005
Posts: 39053
    
  23
I hadn't noticed there already is a throws declaration (sorry), in which case, yes, extends Exception would work all right.
 
GeeCON Prague 2014
 
subject: About exception function!