aspose file tools*
The moose likes Java in General and the fly likes Juit Exception Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Juit Exception Problems" Watch "Juit Exception Problems" New topic
Author

Juit Exception Problems

Thomas Banks
Greenhorn

Joined: Apr 03, 2012
Posts: 10
Hi guys,

I'm relatively new to java and I am currently struggling to get my head around the scanner and throwing exceptions. I have written the
below code but every time I try running my test I get a failure message. Is it possible one of you guy could tell me where Im going wrong?
Thank you.




Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Your test code expects that creating a Triangle, using the constructor of the Triangle class, throws a TriangleException if the input is wrong.

But in the constructor of your class Triangle you're never throwing that exception. The constructor is also not calling the validate method, which might throw the exception. You'll have to call the validate method from the constructor.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Thomas Banks
Greenhorn

Joined: Apr 03, 2012
Posts: 10
Hi thanks for your advice. I have modified my code and now it is throwing an exception in the correct manner.
However, I am now having difficultly in writing the product code to throw the correct exception.
I wish to throw an exception for "too many parameters" and "too few parameters" as we know a triangle should take 3 parameters.
This is what I have come up with so far. The code I thought would work is in my previous post.
Is it possible you could help me throw those two exceptions?

Thanks

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

In line 12, why are you doing new Scanner("0,0,0")? Aren't you supposed to parse the paramenters string that's passed to the constructor?

Look at what your code is doing now:

  • Line 12: Make a scanner to parse the string "0,0,0" (this is most likely not what you meant)
  • Line 15: A loop that iterates over each number found by the scanner
  • Line 16: You get the next number in a variable called num
  • Line 18: If this number is 0, then...
  • Line 20: ...you throw a TriangleException with the message "Too many parameters"

  • This seems to be totally different from what you intend the code to do.

    So, in practice, this means: it's going to parse the string "0,0,0", then in line 16 num gets assigned the first "0" from this string, in line 18 it notices that the number is 0, and in line 20 the exception is thrown.
    Thomas Banks
    Greenhorn

    Joined: Apr 03, 2012
    Posts: 10
    Thank you Sir I have realized the errors of my ways.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Juit Exception Problems