• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exceptons  RSS feed

 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having difficulties getting this to throw an exception for negative numbers. I've gotten it as far as stating length cannot be negative but it continues to convert to centimeters. example:


Enter the length in feet and inches seperated by a space: -5 4

Exception: Length cannot be negative.
The total length converted to centimeters is: -142.24

I tried doing, but didn't work!!


Here's my code:
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is in the do while loop, you're never "done".

Replace
with

[ November 28, 2006: Message edited by: Ta Ri Ki Sun ]
 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After doing as said the results are:

Enter the length in feet and inches seperated by a space:
1 2

The total length converted to centimeters is: 35.56

----jGRASP: operation complete.

----jGRASP exec: java CentimetersExceptionHandling

Enter the length in feet and inches seperated by a space:
-1 -2

-1 -2
Exception java.lang.IllegalArgumentException -1

----jGRASP: operation complete.


I have to enter the negatives twice to get any results and it is displaying the third input(token)...-1. Also the program does need to continue until valid positive numbers are put in.
 
Mattias Ahlin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Modify your code like this and it'll work:


[ November 28, 2006: Message edited by: Mattias Ahlin ]
 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!!! This worked..

Could it also work by placing the exception in its own class? This is what I was trying next. Just curious if that could have been another way. Here's what I was trying:


and also change what I had in CentimetersExceptionHandling and create a test program.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class IllegalArgumentException looks a bit strange.

To start with, IllegalArgumentException is an already existing exception in the package java.lang. It's not a good idea to write your own class with a name that is the same as an already existing, standard Java class.

Also, it's strange that your IllegalArgumentException extends CentimetersExceptionHandling. If you want it to be a real exception class, it should extend class java.lang.Exception.
 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I finally got it to work, if anyone was curious. Not sure if its quite the correct way to work but my results seem to work so far.





Thanks again for everyone that responded. It helps learning different techniques and helps me better understand ways of understanding the logics of it.
 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe sorry people, just realized i posted the code I didn't use. Ignore the "public class ExceptionHandling extends Exception". Wasn't used!! Sorry for any confusion.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!