• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Exceptons

 
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:
 
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.
 
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic