Sunday, 15 September 2013

java - Throws and catch clauses -



java - Throws and catch clauses -

i having problem understanding exception handling, unfortunately confuses me. so, have implement exception codes have been created teacher in code seek , grab blocks. understand bulk of seek , grab blocks, don't know how utilize exceptions have been created , print out info in constructors. 1 of exceptions created

* exception should used when hours worked value hourly employee * found less 1 or greater 84. * * when using exception, if @ possible, pass string containing employee ssn * constructor. ssn displayed part of exception message, * allowing invalid record more located. */ public class invalidhoursworkedexception extends exception { /** * no-arg constructor */ public invalidhoursworkedexception() { super("\ninvalid hours worked"); } /** * next constructor accepts employee's ssn * argument. ssn displayed in error message * exception. */ public invalidhoursworkedexception(string ssn) { super("\nhours worked <1.0 or >84.0 employee cannot processed: " + ssn ); } }

and have far. don't know if it's right nor know how pass ssn variable in set method invalidhoursworkedexception.

public class hourlyemployee extends employee /* * need add together throws clause class header. * list each of exceptions may thrown constructor */ { private double hrsworked; private double hrlyrate; // private double weeklypay; // hourly pay per week // five-argument constructor -- additional arguments hours worked & hourly rate public hourlyemployee( string first, string last, string ssn, double hours, double rate ) throws invalidhoursworkedexception { super( first, last, ssn ); // pass employee constructor /* * before executing each "set" method hours worked, test argument. * if hours worked not fall proper range, throw associated exception, * else, execute set method. */ seek { sethrsworked( hours ); } catch(invalidhoursworkedexception invalidhoursworkedexception) { system.err.printf(invalidhoursworkedexception.getmessage()); } // validate & store hours worked week sethrlyrate( rate ); // validate & store hourly rate }// end of 5 item constructor //set hours worked public void sethrsworked( double hours ) throws invalidhoursworkedexception { if(hours < 0 && hours > 84) throw new invalidhoursworkedexception(); else { hrsworked = hours; } }

this changed code after of comments. still don't know if need throw exception in constructor header , set method header

public hourlyemployee( string first, string last, string ssn, double hours, double rate ) throws invalidhoursworkedexception { super( first, last, ssn ); // pass employee constructor /* * before executing each "set" method hours worked, test argument. * if hours worked not fall proper range, throw associated exception, * else, execute set method. */ seek { sethrsworked( hours ); } catch(invalidhoursworkedexception invalidhoursworkedexception) { throw new invalidhoursworkedexception(ssn); } // validate & store hours worked week sethrlyrate( rate ); // validate & store hourly rate }

i sense set method still off

public void sethrsworked( double hours ) throws invalidhoursworkedexception { if(hours < 0 && hours > 84) throw new invalidhoursworkedexception(); else { hrsworked = hours; } }

ok wanted implement other logic yourself

public class hourlyemployee extends employee { private double hrsworked; private double hrlyrate; public hourlyemployee(string first, string last, string ssn, double hours, double rate) throws invalidhoursworkedexception { super(first, last, ssn); // pass employee constructor seek { sethrsworked(hours); } grab (invalidhoursworkedexception invalidhoursworkedexception) { system.out.println(invalidhoursworkedexception.getmessage()); } // validate & store hourly rate }// end of 5 item constructor //set hours worked public void sethrsworked(double hours) throws invalidhoursworkedexception { if (hours > 0 || hours < 84) throw new invalidhoursworkedexception(); else { hrsworked = hours; system.out.println("hours set "+hrsworked); } } public static void main(string arr[]) { seek { hourlyemployee h = new hourlyemployee("abc","def","xyz",100,10); } grab (invalidhoursworkedexception e) { system.out.println(e.getmessage()); } } }

java exception

No comments:

Post a Comment