Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

About exception function!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Your CalculateException class ought to extend RuntimeException.


You would like to make it an unchecked exception ?
 
Campbell Ritchie
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic