chapter 81 exception handling chapter 8. 2 reminders project 5 due oct 20 @ 10:30 pm project 3...

46
Chapter 8 1 Exception Handling Chapter 8

Post on 20-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 1

Exception Handling

Chapter 8

Page 2: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 2

Reminders

• Project 5 due Oct 20 @ 10:30 pm

• Project 3 regrades due by midnight tonight

• Discussion groups now twice a week (7-9 pm: M in CS G066, W in Phys 11)

Page 3: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 3

Exam 2

• Tuesday, October 25

• 7:00 – 8:00 PM

• Physics 112

• Covers chapters 5-9 (and material from 1-4 as well)

• Same format as exam 1– 20 MC– 5 programming

Page 4: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 4

Introduction

• A program can be written assuming that nothing unusual or incorrect will happen.– The user will always enter an integer when

prompted to do so. – There will always be a nonempty list for a

program that takes an entry from the list.– The file containing the needed information

will always exist.• Unfortunately, it isn’t always so.

Page 5: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 5

Exception Handling

• Either your code or Java signals when something unusual happens.

• The signaling process is called throwing an exception.

• Somewhere in your program, you can place code to handle the exception.

Page 6: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 6

Exceptions in Java: Example, cont.

• simple example, cont.– But what if there is no milk?– An attempt to divide the number of donuts

by the number of glasses of milk will result in an attempt to divide by zero.

– This would be an utter (udder?) disaster, known in Java as an exception.

Page 7: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 7

Exceptions in Java: Example, cont.

• In Java, it is possible to test for this unusual situation using an if-else statement, for example.

Page 8: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 8

Page 9: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 10

Exceptions in Java: Example, cont.

• In Java, it is also possible to throw an exception.

Page 10: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 11

Page 11: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 12

Exceptions in Java: Example, cont.

Page 12: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 13

Defining Exception Classes

• You can define your own exception classes, but they must be derived from an existing exception class.

• Constructors are the most important, and often the only methods (except for methods inherited from the base class).

Page 13: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 14

Defining Exception Classes, cont.

• class DivideByZeroException

Page 14: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 15

Page 15: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 16

Java Tip: Preserve getMessage

• For all predefined exception classes, method getMessage returns either– the string that is passed as an argument to

the constructor or– a default string if no argument is passed to

the constructor.• The behavior of method getMessage should be

preserved in any exception class you define.

Page 16: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 17

Java Tip: Preserve getMessage, cont.

• This is done by including a string parameter that begins with a call to super.public MyException(String message)

{

super(message);

More_Code_If_Appropriate

}

Page 17: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 18

Java Tip: Preserve getMessage, cont.

• Also include a default constructor.public MyException()

{

super(“MyException thrown”);

More_Code_If_Appropriate

}

Page 18: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 19

Programming Tip: When to Define an Exception Class

• In general, define an exception class if you are going to insert a throw statement in your code.

• This permits catch blocks to distinguish between your exceptions and exceptions thrown by predefined methods.

Page 19: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 20

Guidelines

• Use class Exception as the base class unless there is a compelling reason to do otherwise.

• Define at least two constructors. Typically, no other methods are needed.

• Begin each constructor definition with a call to the constructor of the base class.

Page 20: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 21

Declaring Exceptions (Passing the Buck)

• Sometimes is it appropriate to handle an exception other than in the method where the exception occurred.

• For example, it might be better to handle the exception in the method that called the method that called the method… that threw the exception

Page 21: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 22

Declaring Exceptions, cont.

• If a method can throw an exception but does not catch it, it must alert the programmer to the possibility of an exception by including a throws clause.

• Examplepublic void someMethod()

throws DivideByZeroException

Page 22: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 23

Accounting for Exceptions

• An exception can be caught in a catch block within a method definition.

• Alternatively, the possibility of an exception can be declared at the start of the method definition by placing the exception-class name in a throws clause.

• These two approaches can be mixed in a method, catching some exceptions and declaring others in a throws clause.

Page 23: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 24

Accounting for Exceptions, cont.

• If method_A uses a throws clause instead of handling an exception and method_B calls method_A, then method_B either must handle the exception or must also include a throws clause.

Page 24: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 25

Page 25: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 26

Accounting for Exceptions, cont.

• A throws clause can include more than one exception type.

• examplepublic int someMethod()throws

IOException, DivideByZeroException

Page 26: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 27

Accounting for Exceptions, cont.

• Some method in the calling hierarchy should handle the exception.

• If an exception is thrown, but never caught, either the program terminates or its behavior becomes unreliable.

Page 27: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 28

Exceptions That Need Not Be Caught

• Some exceptions do not need to be accounted for in any way.– (Perhaps these are the exceptions that

prove the rule about needing to account for exceptions.)

• Exceptions that do not need to be accounted for result from errors and usually are thrown by methods in predefined classes.

Page 28: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 29

throws Clauses in Derived Classes

• When a method is redefined in a derived class, the redefined method cannot contain any exception classes that are not in the throws clause of the same method in the base class (though the derived class can list fewer exceptions in its throws clause).

• Any exceptions thrown in the derived class must be caught or thrown by the base class.

Page 29: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 30

Multiple Throws and Catches

• A try block can throw any number of different types of exceptions.

• Each catch block can catch only one type of exception.

• Multiple catch blocks after a try block can catch multiple types of exceptions.

Page 30: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 31

Page 31: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 32

Java Tip: Catch the More Specific Exceptions First

• catch blocks are examined in order.• The first matching catch block is executed.• More specific exceptions should precede less

specific exceptions, i.e. exceptions lower in the exception hierarchy should come before exceptions higher in the exception hierarchy.

Page 32: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 33

Keep It Simple

• Attempt to modify a program or class definition so that it does not need a throw statement.

• In general, use exceptions sparingly.• If the way the exception is handled depends

on the calling method, let the calling method handle the exception.

• Consider throwing the exception and catching the exception in separate methods.

Page 33: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 34

Keep It Simple, cont.

public void methodB()

{

...

try

{

...

methodA();

...

}

catch (MyException e)

...

Page 34: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 35

The finally Block

• A finally block can be added after a try block and its catch blocks.

• The finally block is executed– if the try block throws no exceptions– if the try block throws an exception which

is caught by a catch block– if an exception is thrown but not caught

i.e. it is always executed.

Page 35: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 36

The finally Block, cont.

• syntaxtry

{

}

catch Block(s)

finally

{

}

Page 36: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 37

try {

doMethod(“test”);

}

catch (Exception e) {

System.out.println(“Exception”);

return;

}

finally {

System.out.println(“Done”);

}

return;

If doMethod() throws an exception, what is the output?

Page 37: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 38

Output

Exception

Done

Page 38: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 39

Output

Exception

Done

why?

Page 39: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 40

Graphics Supplement: Exceptions in GUIs

• An uncaught exception in a (non-GUI) application will end the program.

• An uncaught exception in a GUI program (either a JFrame GUI or an applet) will not end the program.– However, unless the exception is caught,

the GUI may not cope correctly with the exception or the user may receive insufficient instructions.

Page 40: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 41

Programming Example: A JFrame Using Exceptions

• The example JFrame GUI allows the user to write the name of a color in a text field and to click the “Show Color” button.– If the GUI recognizes the color, the

background color changes to the named color.

– Otherwise, the text field displays “Unknown Color” and the background changes to gray.

Page 41: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 42

Programming Example: A JFrame Using Exceptions, cont.• class ColorDemo

Page 42: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 43

• class UnknownColorException

Programming Example: A JFrame Using Exceptions, cont.

Page 43: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 44

• class ShowColorDemo

Programming Example: A JFrame Using Exceptions, cont.

Page 44: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 45

Programming Example: A JFrame Using Exceptions, cont.

Page 45: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 46

throws Clause Not Allowed in actionPerformed

• A throws clause cannot be added to method actionPerformed in any action listener class.

• Any exception thrown in method actionPerformed must be caught in method actionPerformed.

• Similarly, if method windowClosing is redefined in a window listener class, you may not add a throws clause to method windowClosing.

Page 46: Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 20 @ 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now

Chapter 8 47

Summary

• You have become familiar with the notion of exception handling.

• You have learned Java syntax for exception handling.

• You have learned to use exception handling effectively in classes and programs.