Win a copy of Spring Boot in Practice this week in the Spring 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Exceptons

 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic