• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Exception Constructor Error

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,

I am getting the error "Constructor NegativeException() is undefined" in my Root class. Can anyone point me in the right direction?



SAMPLE QUESTION

Attempting to get the square root of a negative number is an illegal mathematical operation. Java supplies a square root method in the Math class i.e.:

public static double sqrt(double a)

This method returns a special constant double value (NaN i.e. not a number) if the parameter had a negative value. It does not throw an exception.

a) Write the Java code for a custom exception class to represent an exception resulting from an attempt to get the square root of a negative number (double). This class should inherit from ArithmeticException, and only needs a simple constructor whose parameter holds the value of the number which has caused the exception.



b) Write the Java code for a Root class which contains a sqrt method which will return the square root of a double passed to it as a parameter or throw an exception of the type defined in (a) if the parameter is negative. It should call Math.sqrt() if no exception occurs.


c) Write the code for a main method in a test class which calls the method defined in (b), inside a try/catch block.









 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eclipse prompted me to add another empty constructor for NegativeException and it worked! not really sure whats going on there
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'throw new NegativeException();' fails to compile as you don't have a no-arg constructor in NegativeException class. You need to modify the call as 'throw new NegativeException(number);'.
Your Root class won't compile even after this as its superclass NegativeException doesn't have a no-arg constructor. Since you didn't define a constructor for Root class, compiler provides a default one with an implicit call to super(), where it fails. In this case, Root doesn't need to extend from NegativeException.
 
Marshal
Posts: 77291
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does Root extend NegativeException? That looks totally inappropriate.
I would have thought NegativeException should extend IllegalArgumentException, but I would point out that the sqrt() method will accept a negative argument. Have a look at its documentation, and you will find out what it returns for a negative argument.
 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys, I made the changes suggested and the exception get thrown correctly now. Richie, this is a past paper question i'm working from, if the square root of a negative number returns something does this mean the question is incorrect?

The last part of the question asks to use a try,catch block, can I add this even when iv'e manually thrown the exception already?





 
Campbell Ritchie
Marshal
Posts: 77291
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you look at the documentation? Here it is. You have a choice between throwing an Exception for a negative number, returning what the Math class method does, or converting it to a positive number and returning a real number. Obviously different people will make different choices.
You can put a try-catch in the method where you call the root method from. Don’t throw and catch the same Exception in the same place.
 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Richie, try catch worked from the test class. I had a look at the documentation but it doesn't really make sense to me.
 
Campbell Ritchie
Marshal
Posts: 77291
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome Did you find the Java Tutorials? That might help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic