I am having trouble understanding exception handling, unfortunately it just really confuses me. So, I have to implement exception codes that have been created by my teacher in another code with try and catch blocks. I understand the majority of the try and catch blocks, but I don't know how to use the exceptions that have been created and print out the informtation in their constructors. This is one of the excpetions that she created
public class InvalidHoursWorkedException extends Exception
{
/**
* No-arg constructor
*/
public InvalidHoursWorkedException()
{
super("\nInvalid Hours Worked");
}
/**
* The following constructor accepts the employee's SSN
* as an argument. The SSN will be displayed in the error message
* for the exception.
*/
public InvalidHoursWorkedException(String ssn)
{
super("\nHours Worked are <1.0 or >84.0 Employee cannot be processed: " + ssn );
}
}
and this is the program that I have to put the try and catch blocks in. As you can see I tried to start it and I know I need an if statement for the hours worked, but I am lost on the placement of it I suppose and how to have the information printed in the exception constructor
/*
* HourlyEmployee
*/
public class HourlyEmployee extends Employee
/*
* You will need to add a throws clause to the class header.
* List each of the exceptions that may be thrown by the constructor
*/
{
private double hrsWorked;
private double hrlyRate;
// private double weeklyPay; // hourly pay per week
// five-argument constructor -- additional arguments are hours worked & hourly rate
public HourlyEmployee( String first, String last, String ssn,
double hours, double rate ) throws InvalidHoursWorkedException
{
super( first, last, ssn ); // pass to Employee constructor
/*
* Before executing each "set" method for hours worked, test the argument.
* If hours worked does not fall into the proper range, throw the associated exception,
* else, execute the set method.
*/
try
{
if(hrsWorked > 0 & <= 84)
{
setHrsWorked( hours ); // validate & store hours worked this week
setHrlyRate( rate ); // validate & store hourly rate
}
catch(InvalidHoursWorkedException invalidHoursWorkedException)
{
System.err.printf("%s\n", invalidHoursWorkedException.getMessage(ssn);
} // end of five item constructor
//set hours worked
public void setHrsWorked( double hours )
{
hrsWorked = hours;
}
//return hours worked
public double getHrsWorked()
{
return hrsWorked;
} // end return hours worked
//set hourly rate
public void setHrlyRate( double rate )
{
hrlyRate = rate;
} // end set hourly rate
//return hourly rate
public double getHrlyRate()
{
return hrlyRate;
} //end return hourly rate
// calcualte weekly pay
public double calcWeeklyPay( double hrsWorked, double hrlyRate )
{
return hrsWorked * hrlyRate;
} // end method calcWeeklyPay
// return String representation of HourlyEmployee object
@Override
public String toString()
{
return String.format( "\nHourly Employee: %s\n%s: %.2f %s $%.2f",
super.toString(), "Hours worked", getHrsWorked(), " at rate ", getHrlyRate() );
} // end method toString
} // end class HourlyEmployee
I am sorry if it's a lot, but this just goes right over my head.