14b exceptions


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

14b exceptions

  1. 1. Finally <br />LIS4930 © PIC<br />There are times when you want some code to run regardless of an exception – you want it to run no matter what!<br />The finally block is where you put code that must run regardless of an exception.<br />A finally block lets you put all your important cleanup code in one place instead of duplicating it like this:<br />try {<br />turnOvenOn( );<br />x.bake( );<br />turnOvenOff( );<br />} catch(BakingException ex) {<br />ex.printStackTrace();<br />turnOvenOff( );<br />}<br />try {<br />turnOvenOn( );<br />x.bake( );<br />} catch(BakingException ex) {<br />ex.printStackTrace();<br />} finally {<br />turnOvenOff( );<br />}<br />
  2. 2. A Method Can Throw More Than One Exception<br />LIS4930 © PIC<br />public class Laundry {<br /> public void doLaundry( ) throws PantsException, ShoeException {<br /> // code that could throw either exception<br /> }<br />}<br />public class Foo {<br /> public void go( ) {<br /> Laundry laundry = new Laundry( );<br /> try {<br />laundry.doLaundry( );<br /> } catch(PantsException ex) {<br /> // recovery code<br /> } catch(ShoeException ex) {<br /> // recovery code<br /> }<br /> }<br />}<br />
  3. 3. Exceptions Are Polymorphic<br />LIS4930 © PIC<br />Don’t forget Exceptions are objects so a ClothingException can be extended into ShirtExceptions, PantsExceptions, and DressExceptions. Therefore:<br />1<br />2<br />You can DECLARE exceptions using a supertype of the exceptions you throw.<br />You can CATCH exceptions using a supertype of the exception thrown.<br />Just because you CAN catch everything with one big super polymorphic catch, doesn’t mean you SHOULD.<br />Write a different catch block for each exception that you need to handle uniquely.<br />Look at page 330 for an example of polymorphic exceptions<br />
  4. 4. Catching Multiple Exceptions<br />Multiple catch blocks must be ordered from smallest to biggest.<br />You can’t put bigger baskets above smaller baskets.<br />LIS4930 © PIC<br />Don’t do this!<br />try {<br />laundry.doLaundry( )<br />} catch (ClothingExceptionce) {<br /> // recovery code goes here<br />} catch (ShoeException se ) {<br /> // recovery code goes here<br />} catch (DressException de) {<br /> // recovery code goes here<br />}<br />
  5. 5. Paying It Forward<br />LIS4930 © PIC<br />If you don’t want to handle an exception you can just throw it yourself so that whomever calls YOU will have to handle the exception. <br />If you call a risky method that does throw an exception, instead of you handling it, you can keep throwing it.<br />public void foo( ) throws PantsException, ShoeException {<br /> // call risky method without a try/catch block<br />laundry.doLaundry( );<br />} <br />
  6. 6. Ducking (by paying it forward) Only Delays the Inevitable<br />LIS4930 © PIC<br />public class Washer {<br /> Laundry laundry = new Laundry( );<br /> public void foo( ) throws ClothingException{<br />laundry.doLaundry( );<br /> }<br /> public static void main(String[] args) throws ClothingException{<br /> Washer a = new Washer( );<br />a.foo( );<br /> }<br />}<br />1<br />2<br />3<br />4<br />doLaundry( ) throws a ClothingException<br />foo( ) ducks the exception<br />main( ) ducks the exception<br />The JVM shuts down<br />
  7. 7. Handle or Declare!<br />LIS4930 © PIC<br />So now we’ve seen both ways to satisfy the compiler when you call a risky (exception-throwing) method.<br />1<br />2<br />HANDLE – Wrap the risky call in a try/catch block<br />DECLARE – duck it / pay it forward<br />Let’s look at the sequencer to see how each method works.<br />
  8. 8. Exception Rules<br />LIS4930 © PIC<br />1<br />2<br />3<br />4<br />You cannot have a catch or finally without a try<br />You cannot put code between the try and the catch<br />A try MUST be followed by either a catch or a finally<br />A try with only a finally (no catch) must still declare the exception<br />void go ( ) {<br />Foof = new Foo( );<br />f.foof( );<br />catch(fooException ex) { }<br />}<br />try {<br />x.doStuff( );<br />} finally {<br /> // cleanup<br />}<br />try {<br />x.doStuff( );<br />} <br />inty = 43;<br />} catch (Exception ex ) { }<br />void go ( ) throws FooException {<br /> try {<br />x.doStuff( );<br /> } finally { }<br />}<br />
  9. 9. Familiar Example<br />LIS4930 © PIC<br />But, what about this?<br />We now know what this means.<br />
  10. 10. Input and Output<br />The java.iopackage includes a rich collection of different classes to support I/O.<br />Different classes provide different ways for programs to organize and retrieve data.<br />Java programs do not communicate directly with external devices, instead they create a stream object to connect the program to the device. Each stream functions as a conduit that establishes a path for the data to flow between the program and the I/O device.<br />LIS4930 © PIC<br />
  11. 11. Streams<br />Java supports several different streams for different purposes.<br />LIS4930 © PIC<br />Output Stream<br />Executing Program<br />File (on disk)<br />Input Stream<br />Executing Program<br />File (on disk)<br />
  12. 12. Stream Hierarchy<br />LIS4930 © PIC<br />Output Stream (abstract)<br />Input Stream (abstract)<br />FileOutputStream<br />FileInputStream<br />to write raw bytes<br />to read raw bytes<br />ObjectOutputStream<br />ObjectInputStream<br />to write whole objects<br />to read whole objects<br />FilterOutputStream<br />FilterInputStream<br />DataOutputStream<br />DataInputStream<br />to write primitive values <br />to read primitive values <br />
  13. 13. Using Streams<br />Open the file for input, instantiating associated stream objects.<br />Call read methods to retrieve part of or the entire stream’s content.<br />Close the file/stream.<br />LIS4930 © PIC<br />Input Streams<br />Output Streams<br />Open the file for output, instantiating associated stream objects.<br />Call read methods to write data into the stream.<br />Close the file/stream.<br />
  14. 14. DataInputStreams & DataOutputStreams<br />LIS4930 © PIC<br />FileOutputStream<br />FileInputStream<br />DataOutputStream<br />DataInputStream<br />Executing Program<br />Executing Program<br />File (on disk)<br />File (on disk)<br />
  15. 15. Text Files<br />LIS4930 © PIC<br />Input Stream<br />Input Stream<br />Output Stream<br />Output Stream<br />BufferedReader<br />Writer<br />Reader<br />Executing Program<br />Executing Program<br />Executing Program<br />Executing Program<br />Reader<br />Writer<br />File (on disk)<br />File (on disk)<br />File (on disk)<br />File (on disk)<br />BufferedWriter<br />
  16. 16. Familiar Example<br />LIS4930 © PIC<br />Now we know what this means<br />What if we didn’t “duck” the exceptions?<br />
  17. 17. Input and Output of Files<br />LIS4930 © PIC<br />Input Stream<br />Input Stream<br />Output Stream<br />Use a FileReader/FileWriter objects as your “Reader” conduit.<br />BufferedReader<br />FileReader<br />Executing Program<br />Executing Program<br />Executing Program<br />FileReader<br />FileWriter<br />File (on disk)<br />File (on disk)<br />File (on disk)<br />BufferedWriter<br />