• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

? Throw an exception vs other approach

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings all.

I'm cutting my Java teeth on an application that performs a medical calculation to estimate a person's kidney function. I can get the relevant values and calculate the result but I'd like to add range-checking (so for instance, if the object is passed a patient's age of 150 my object's calulating method will not return an erroneous result). I'm a purist at heart - I'd like to know the *best* way to do the range checking - should I learn about exceptions at this point or is another approach preferred?

I'm learning from a combination of HeadFirst Java and AgileJava at this point; I think they are both excellent.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, I don't think there is a cut-and-dry answer to your question. For one thing, it depends on who answers it. I'm certain that you will get a variety of opinions in this thread.

Personally, I would like to start by seeing your code so far. There may be an elegant way to add the range-checking without introducing exception handling.

On the other hand, this might be a good opportunity to get your feet wet. It sounds like simple enough program that you should be able to use it to learn about how exceptions work. In the end, I think experience is the best guide to indicate when exceptions are a good idea and when they are not. So I think it will help if you use exceptions. If they end up not working very well, then at least you may learn how to NOT use exceptions.

Layne
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Layne. For simple, direct solutions, your choices are:

a) throw an exception
b) return an invaid number (-1)
c) return a null object

Any way you slice it, either the calling method or the called method has to do extra work for out-of-range input, so it's best to start with there you think the responsibility is best implemented.

For user input, I favor handling range-checking in the calling method. If the called method is already designed to handle it, then go with that.
 
James Hejmanowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the input. I'll post the code (already 92 lines without the tests) ASAP; I look forward to seeing how you folks would approach it.

I can already see clearly how to handle the problem via returning null objects or (-1) results so I'm leaning towards exceptions as I have the most to learn from this approach
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fail early.
If you have a finite set of values, declare them as an enumeration, such that you have type-safety (fail before it even turns into executable bytecode). If you don't, then I don't know what you're talking about; I am guessing.
 
James Hejmanowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony; I'll declare gender as enumeration, other values are ints and floats as noted below.

What I've got so far is:



// 9/26/05 james Hejmanowski
package medcalc;
class Patient {
String name = "null";
int age = 0;
float serumCreatinine = 0, heightInInches = 0;
String gender = "null";
// Constructor - Name only.
Patient( String name ){
this.name = name;
}
// Constructor - the whole enchilada
Patient( String name, String gender, int age, float heightInInches,
float serumCreatinine){
this.name = name;
this.gender = gender;
this.age = age;
this.heightInInches = heightInInches;
this.serumCreatinine = serumCreatinine;
}
// Estimate CrCl using gender, height, serum creatinine by Cockroft/Gault
// method.
float EstimateCrCl(){
if ( gender == "Male" ){
float estCrCl = 0f;
float iBW = CalculateIdealBodyWeight();
estCrCl = ( ( ( 140 - age ) * iBW )/( 72 * serumCreatinine ) );
return estCrCl;
}
if ( gender == "Female" ){
float estCrCl = 0f;
float iBW = CalculateIdealBodyWeight();
estCrCl = 0.85f * ( ( ( 140 - age ) * iBW )/
( 72 * serumCreatinine ) );
return estCrCl;

}
// return of 0 indicates gender not set properly.
return 0f;
}
// CalculateIdealBodyWeight relies on heightInInches and
// gender having been set to other than default values
public float CalculateIdealBodyWeight() {
if ( heightInInches != 0 || gender != "null" ) {
if ( gender == "Male" ){
if ( heightInInches >= 60f ){
return (float)( 50f + ( ( heightInInches - 60f ) * 2.3f ) );
}
}
if ( gender == "Female" ){
if ( heightInInches >= 60f ) {
return(float)( 45.5f + ( ( heightInInches - 60f ) *
2.3f ) );
}
}
else return 0.1f;
}
return 0.0f;
}
// Begin Getters and Setters
String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
// Test setting and getting gender
public void setGender(String gender){
this.gender = gender;
}
String getGender(){
return gender;
}

public void setHeightInInches(float heightInInches){
this.heightInInches = heightInInches;
}

public float getHeightInInches(){
return heightInInches;
}

public void setSerumCreatinine(float serumCreatinine){
this.serumCreatinine = serumCreatinine;
}

public float getSerumCreatinine(){
return serumCreatinine;
}
// End Getters and Setters.
}
 
James Hejmanowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I haven't figured out how to paste text into this forum and preserve the whitespace/formatting
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would absolutely use Exceptions all over the place. At its simplest level, just use the java.lang.Exception class. Or, to get complex, develop your own exception class which subclasses exceptions. In any cae, declare your methods to throw exceptions that need some sort of validation on them, especially your getters and setters.

i.e.:
public void setAge(int age) throws Exception {
if (age < 0) {
throw new Exception("Age cannot be less than 0");
}

this.age = age;
}



Now, onto gender...
I would use static variables in your class:
public static final int MALE = 1;
public static final int FEMALE = 2;

public void setGender(int gender) throws Exception {
if (gender != thi.MALE && gender != this.FEMALE) {
throw new Exception("Gender is invalid.");
}

this.gender = gender;
}


And then when using this class, one could just do:
patient.setGender(Patient.MALE);

Tim
 
Tim Archer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would absolutely use Exceptions all over the place. At its simplest level, just use the java.lang.Exception class. Or, to get complex, develop your own exception class which subclasses exceptions. In any cae, declare your methods to throw exceptions that need some sort of validation on them, especially your getters and setters.

i.e.:
public void setAge(int age) throws Exception {
if (age < 0) {
throw new Exception("Age cannot be less than 0");
}

this.age = age;
}



Now, onto gender...
I would use static variables in your class:
public static final int MALE = 1;
public static final int FEMALE = 2;

public void setGender(int gender) throws Exception {
if (gender != thi.MALE && gender != this.FEMALE) {
throw new Exception("Gender is invalid.");
}

this.gender = gender;
}


And then when using this class, one could just do:
patient.setGender(Patient.MALE);

Tim
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Hejmanowski:
Sorry. I haven't figured out how to paste text into this forum and preserve the whitespace/formatting



There are buttons below the message text area that help you do this. Click on the one labeled CODE and past your code in between the tags that are inserted into your message. You should also check out the help link for UBB Code tags. The link is to the left of the message text area.

Layne
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic