Lecture18

Lecture18 - A method can throw more than one exception....

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 A method can throw more than one exception. public class Laundry { public void doLaundry() throws PantsException, LingerieException { // code that could throw either exception } } public class Foo { public void go() { Laundry laundry = new Laundry (); try { laundry.doLaundry(); } catch(PantsException pex) { // recovery code } catch(LingerieException lex) { // recovery code } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Exceptions are polymorphic You can declare exceptions using a supertype of the exceptions you throw. public void doLaundry() throws ClothingException // ClosingException lets you throw any //subclass of ClothException.
Background image of page 2
3 You can Catch exceptions using a supertype of the exception thrown. try { laundry.doLaundry(); } catch (ClothingException cex) { Can catch any ClothingException subclass try { laundry.doLaundry(); } catch (ShirtException cex) { Can catch only TeeShirtException and DressShirtException
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Just because you CAN catch everything with one big super polymorphic catch, doesn't always mean you SHOULD. try { laundry.doLaundry(); } catch (Exception ex) { // recovery code … } This catch block will catch any exceptions, so you won’t automatically know what went wrong. Write a different catch block for each exception that you need to handle uniquely try { laundry.doLaundry() ; } catch (PantsException tex) { // recovery from TeeShirtException } catch (LingerieException lex) { // recovery from LingerieException } catch (ClothingException cex) { // recovery from ClothingException } public class Laundry { public void doLaundry() throws PantsException, LingerieException { // code that could throw either exception } }
Background image of page 4
5 Multiple catch blocks must be ordered Don’t do this! try { laundry. doLaundry (); // recovery from ClothingException } catch(ClothingException cex) { } catch(LingerieException lex) { // recovery from LingerieException } catch(ShirtException sex) { // recovery from ShirtException }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 When you don’t want to handle an exception, just duck it by declaring it. public void foo() throws ReallyBadException { // call risky method without a try/catch laundry.doLaundry(); } Since you don’t have a try/catch for the risky method you call, you are now the “risky method”. Whoever calls you has to deal with the exception. Sooner or later, somebody has to deal with it. But what if main() ducks the exception? public class Washer { Laundry laundry = new Laundry(); public void foo() throws ClothingException { laundry.doLaundry(); } public static void main (String[] args) throws ClothingException { Washer a = new Washer (); a.foo (); }} This compiles just fine! The JVM shuts down!
Background image of page 6
7 Handle or Declare. It's the law. (1) Handle Wrap the risky call in a try/catch try { laundry.doLaunday(); } catch (ClothingException cex) ( // recovery code } (2) Decalre (duck it) Declare that your method throws the same exceptions as the risky method you're calling, void foo() throws ClothingException { launday.doLaundry(); }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 If foo() ducks the exception (by declaring it), and main() calls foo(), then main() has to
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 22

Lecture18 - A method can throw more than one exception....

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online