Exception Handling - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Exception Handling

Description:

It is a reference to the IOException object that was 'thrown' by System.in.read ... throws IOException, NullPointerException, IndexOutOfBoundsException; ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 16
Provided by: AFG4
Category:

less

Transcript and Presenter's Notes

Title: Exception Handling


1
Exception Handling
  • Separating exception-handling blocks of code from
    normal code

2
Java Errors
  • Methods that might experience an error (such as a
    device not being available during read( ) ), are
    written to throw an Exception.
  • This means that, if a read error occurs, a
    special type of object (Exception object) is
    created.
  • The Exception object contains information about
    the error that occurred.
  • Java Requires that Exceptions be caught and
    handled by any program using this method.
  • You cant ignore exceptions

3
trycatch blocks
  • try some code that could produce (throw) an
    error (Example a call to System.in.read( ) ),
  • then catch the error the method throws (create a
    reference to the Exception Object that contains
    information about the error)
  • and handle the error, if it occurs.

4
  • try // try block is normal
    code
  • System.in.read()
  • catch ( IOException ioe) //if error occurs
  • // use the IOException object to see the
    error
  • System.err.println( read error ioe )

5
catching Exceptions
  • ioe is a local variable
  • It is a reference to the IOException object that
    was thrown by System.in.read()
  • It tells all about the exception that occurred.
  • Here, we print ioe to standard error...
  • catch ( IOException ioe) //if error occurs
  • // use the IOException object to see error
  • System.err.println( read error ioe )

6
How do You KnowWhat Exceptions to catch?
  • Java methods that are written to throw an
    Exception, indicate it in the method signature
  • public int read() throws IOException
  • public int read(byte b) throws IOException,
  • NullPointerException
  • public int read(byte b, int off, int len)
  • throws IOException,
  • NullPointerException,
  • IndexOutOfBoundsException

7
Catching multiple Exceptions
  • byte b new byte10
  • try
  • System.in.read(b) //this throws more
  • //than one Exception
  • catch (IOException e1)
  • System.err.println(read err e1)
  • catch (NullPointerException e2)
  • System.err.println(array err e2)

8
Throwing again - Instead of catching
  • public class MainDriver
  • public static void main(String args) throws
    IOException
  • byte my_b
  • System.out.println(enter a
    character)
  • my_b System.in.read( )
  • // System.in.read( ) throws
    IOException, and it MUST
  • // either be caught or passed on
    (thrown) to the next class.
  • // thats why we add the throws
    clause on our main method
  • // it is a trick to keep us from
    having to catch and handle
  • // this error right here. We just
    throw it on.

9
The finally blockExecutes no matter what
  • try
  • System.in.read()
  • catch ( IOException ioe)
  • System.err.println( read error ioe )
  • finally
  • System.out.println(we did it!)

10
Partial Java Exception Class Hierarchy
  • Exception
  • RuntimeException
  • IndexOutOfBoundsException
  • NullPointerException
  • IOException
  • EOFException
  • FileNotFoundException
  • InterruptedIOException

11
Throwing Your Own Exceptions
  • public class Account
  • . . .
  • public void withdraw (double amount) throws

  • InsufficientFundException
  • if ( balance lt amount )
  • throw new InsufficientFundExceptio
    n( this )
  • else
  • balance balance amount
  • . . .

12
Catching Your Own Exceptions
  • public static void main( String args )
  • Account acc new Account( 100.00)
  • . . .
  • //take care of some transactions
  • . . .
  • try
  • acc.withdraw(10.00)
  • catch (InsufficientFundException ex)
  • System.out.println( ex )

13
Creating Your Own Exception Classes
  • public class InsufficientFundException extends
    Exception
  • private Account account
  • public InsufficientFundException( Account a )
  • super("Insufficient Funds")
  • account a
  • account.applyAdminCharge( 18.00 )

14
Lab
  • Complete Account Exception Program

15
Project 3
  • Start with the code we did in Lab 3.
    Animal.java, Dog.java, Cat.java and
    AnimalTest.java. You can get copies of it from
    the Web Site.
  • For the Dog class, add a boolean attribute called
    treat. Add a setter method called setTreat(
    boolean t ). Add a getter method called
    isTreat. In the speak( ) method, add code to
    check the value of treat (use the getter method
    isTreat). If the dog does not have a treat,
    throw the UnableToSpeakException.
  • For the Cat class, add a boolean attribute called
    hairball. Add a setter method called
    setHairBall( boolean h ). Add a getter method
    called isHairBall. In the speak( ) method, add
    code to check the value of hairball (use the
    getter method isHairBall). If the cat has a
    hairball throw the UnableToSpeakException.
  • Write a new Exception class called
    UnableToSpeakException. Handle the
    UnableToSpeakException by printing to the screen
    that the Animal is unable to speak. If the
    object throwing the Exception is a dog, mention
    that the dog needs a treat before speaking. If
    the object throwing the Exception is a cat,
    suggest to the user that they remove the poor
    cats hairball.
  • In the AnimalTest program, test your Exception
    class by causing your new Exception to be thrown
    from a Dog and a Cat.
  • Turn in a hard copy of all your classes
    Animal.java, Dog.java, Cat.java, AnimalTest.java
    and UnableToSpeakException. Turn in a copy of
    your output and your disk.
Write a Comment
User Comments (0)
About PowerShow.com