Prev | Current Page 224 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

out.println( ex.getMessage() );
}
System.out.println( family + " " + family.getSpeed() );
System.out.println( sports + " " + sports.getSpeed() );
}
In this case, the catch block simply reports the error, and the program continues on. With other errors, the
catch block might try to correct the problem, ask the user for a decision, or simply terminate the program by
calling System.exit().
The output from this version of AutomobileFactory looks like this:
3 Automobiles
2006 Kia Rio
2002 VW Passat
2005 Ford Mustang
New speed exceeds maximum speed of 2002 VW Passat
2002 VW Passat 0.0
2005 Ford Mustang 0.0
86 PROGRAMMING IN JAVA [CHAP. 5
When AutomobileFactory tries to accelerate the VW to too great a speed, the Automobile class
throws an ExcessiveSpeedException which stops execution of accelerate() and transfers control
to the catch block. The catch block reports the problem by printing the message attribute of the Exception
object. When the catch block completes, the program continues, but the speeds of both Automobiles remain
0.0, because the path of execution never set the speed of either one.
There can be more than one catch block; in fact, you can have several. Each one can specify a particular
class of Exception to handle.


Pages:
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
www.books61.hobbitstory.com tanie noclegi międzyzdroje pokoje windsurfing Władysławowo noclegi e-lady