File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ? Throw an exception vs other approach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "? Throw an exception vs other approach" Watch "? Throw an exception vs other approach" New topic
Author

? Throw an exception vs other approach

James Hejmanowski
Greenhorn

Joined: Aug 22, 2005
Posts: 8
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.


"I drank what"?<br /> -Socrates
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
James Hejmanowski
Greenhorn

Joined: Aug 22, 2005
Posts: 8
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
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
James Hejmanowski
Greenhorn

Joined: Aug 22, 2005
Posts: 8
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

Joined: Aug 22, 2005
Posts: 8
Sorry. I haven't figured out how to paste text into this forum and preserve the whitespace/formatting
Tim Archer
Greenhorn

Joined: Sep 30, 2005
Posts: 2
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

Joined: Sep 30, 2005
Posts: 2
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

Joined: Dec 06, 2001
Posts: 3061
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ? Throw an exception vs other approach
 
Similar Threads
Portable J2EE 1.4 Web Services?
New Comer
Plotting points for a polygon
Folder and file attributes
Inserting table and image into a doc file using hwpf(poi)