Top Banner
Exceptions Exceptions CSC 171 FALL 2004 LECTURE 24
48

Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Jan 03, 2016

Download

Documents

Elwin Boyd
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

ExceptionsExceptions

CSC 171 FALL 2004

LECTURE 24

Page 2: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

READINGREADING

Read Horstmann Chapter 14This course covered Horstmann Chapters 1

- 15

Page 3: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

EXAMEXAM

Thursday 12/9 in class– Chapters 11, 13-15

Page 4: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Make up examMake up exam

Friday 12/3 12:15PM-1:25PMCSB 703

Page 5: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

GRADINGGRADING

OLD Midterm 10% Projects (4) 40% Final 15% Quizes (10) 10% Labs (15) 15% Workshops 10%

NEW Exam 1 13% Projects (4) 40% Exam 2 12% Quizes (6) 5% Labs (21) 20% Workshops 10%

Page 6: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Errors in ProgrammingErrors in Programming

Page 7: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Errors in ProgrammingErrors in Programming

Sometimes cause by our codeSometimes caused by external codeReasonable to take precautions

Page 8: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Two aspects of errorsTwo aspects of errors

Recognizing when an error occurs

Dealing with the errors

Page 9: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

There are two aspects of handling failure: ________________ and ________________.

Page 10: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

There are two aspects of handling failure: ___detection____ and ____recovery____________.

Page 11: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Old SchoolOld School

have the method return an indication of success/failure (an “error code”)

x.doSomething();

// becomes

if (x.doSomething() == -1) return false;

Page 12: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

New SchoolNew School

Java has an exception handling mechanism which can require potential errors to be recognized.

This mechanism is flexible and efficient.

Page 13: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

In Java, ___________________________ provides a flexible mechanism for passing control from the point of error detection to a recovery handler.

Page 14: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

In Java, __exception handling_______ provides a flexible mechanism for passing control from the point of error detection to a recovery handler.

Page 15: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Exception ObjectsException Objects

Page 16: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

THROWING EXCEPTIONSTHROWING EXCEPTIONSIf I am a method

AND If a problem occurs

then I can deal with it by :

1. Constructing a new exception object describing the problem

2. “Throw” the object to the method that invoked me (I have to terminate to do this)

Page 17: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

ExampleExample

public class BankAccount { public void withdraw(double amount) { if (amount > balance) throw new IllegalArgumentException( "Amount exceeds balance"); balance = balance - amount; } ...

}

Page 18: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

To signal an exceptional condition, use the _____________ statement to throw an _____________________ object.

Page 19: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

To signal an exceptional condition, use the ____throw____ statement to throw an __________exception____ object.

Page 20: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

When you throw an exception, the current method _____________________.

Page 21: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

When you throw an exception, the current method __terminates______.

Page 22: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

CHECKED/UNCHECKEDCHECKED/UNCHECKED

It’s hard to anticipate all possible exceptions at compile time.

The compiler checks to see if we deal with exceptions (checked exeptions)

Unchecked exceptions are not enforced by the compiler.

Page 23: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException ;

public class Console {

public static void main(String [] args) {

InputStreamReader isreader = new InputStreamReader(System.in);

BufferedReader console = new BufferedReader(isreader);

String input1 = console.readLine();

System.out.println(input1);}}

Page 24: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException ;

public class Console {

public static void main(String [] args) throws IOException{

InputStreamReader isreader = new InputStreamReader(System.in);

BufferedReader console = new BufferedReader(isreader);

String input1 = console.readLine();

System.out.println(input1);}}

Page 25: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException ;public class Console {public static void main(String [] args) {

InputStreamReader isreader = new InputStreamReader(System.in);BufferedReader console = new BufferedReader(isreader);

try { String input1 = console.readLine(); System.out.println(input1);

} catch (IOException e) {

System.out.println(“Problem”);}

}}

Page 26: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Unchecked ExceptionsUnchecked Exceptions

int k = Integer.parseInt(“Hello World”);

int [] a = {2,3,4,5,6,7,8,9} ;

a[20] = 5;

Page 27: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

There are two kind of exceptions: ______________________ and _____________________ exceptions.

Page 28: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

There are two kind of exceptions: ___checked_______ and ___unchecked________ exceptions.

Page 29: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Unchecked exceptions extend the class _________________________ or _________________.

Page 30: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Unchecked exceptions extend the class _RuntimeException______ or _____Error______.

Page 31: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Checked exceptions are due to __________________________________. The compiler checks that your program handles these exceptions.

Page 32: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Checked exceptions are due to _external circumstances___. The compiler checks that your program handles these exceptions.

Page 33: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Add a _____________________ specifier to a method that can throw a checked exception.

Page 34: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Add a __throws_______ specifier to a method that can throw a checked exception.

Page 35: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.
Page 36: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

You can design your own exception types – subclasses of _______________________ or ______________________.

Page 37: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

You can design your own exception types – subclasses of __Exception____ or ___RuntimeException_.

Page 38: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

public class InsufficientFundsException extends RuntimeException { public InsufficientFundsException() { }

 public InsufficientFundsException(String reason) { super(reason); }

}

Page 39: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

TRY/CATCHTRY/CATCH

Statements in try block are executed If no exceptions occur, catch clauses are skipped If exception of matching type occurs, execution

jumps to catch clause If exception of another type occurs, it is thrown to

the calling method If main doesn't catch an exception, the program

terminates with a stack trace

Page 40: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); System.out.println("How old are you?"); String inputLine = in.readLine(); int age = Integer.parseInt(inputLine); age++; System.out.println("Next year,you'll be " + age);

} catch (IOException exception) {

System.out.println("Input/output error " +exception);} catch (NumberFormatException exception) {

System.out.println("Input was not a number"); }

Page 41: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

In a method that is ready to handle a particular exception type, place the statements that can cause the exception inside a ____________________, place the handler inside a ______________________________.

Page 42: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

In a method that is ready to handle a particular exception type, place the statements that can cause the exception inside a ________try block________, place the handler inside a __catch clause_________.

Page 43: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

It is better to _________________________________ than to _______________________.

Page 44: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

It is better to ________give_______________ than to ________ receive _________.

Page 45: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

finallyfinally

Exception terminates current method Danger: Can skip over essential code Example:

BufferedReader in; in = new BufferedReader(   new FileReader(filename)); purse.read(in); in.close(); 

Must execute in.close() even if exception happens Use finally clause for code that must be executed "no

matter what"

Page 46: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Once a try block is entered, the statements in a ___________________ clause are guaranteed to be executed, whether or not an exception is thrown.

Page 47: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

Once a try block is entered, the statements in a ______ finally _____ clause are guaranteed to be executed, whether or not an exception is thrown.

Page 48: Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters 1 - 15.

ExampleExample

BufferedReader in = null; try { in = new BufferedReader( new FileReader(filename)); purse.read(in); } finally { if (in !=null) in.close(); }