19
Exceptions cs1043

Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Embed Size (px)

Citation preview

Page 1: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Exceptions

cs1043

Page 2: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Program Exceptions

• When a program detects an error, what should it do?– Nothing, simply allow the program to fail.– Implement a course of action to correct the error.

Page 3: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Example:

• A user enters an incorrect URL into a web browser.– Should the browser crash?– Should the browser request another URL?

Page 4: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Exception Handling

• Software is designed to detect and correct most failures.

• Detection and correction of an error is known as exception handling.

Page 5: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Java Exceptions

• The Java compiler places exceptions into two categories:– Checked exceptions– Unchecked exceptions

Page 6: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Checked Exceptions

• The compiler requires the programmer to either:1. write a competent exception handler (CEH) for

any checked exceptions. - Example: Input-output operations (reading and writing

files and data streams).

2. Throw the exception hoping another method in the calling-tree will be able to handle the exception. This require the throws keyword.

Page 7: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Unchecked Exceptions

– Example

double z = x / y; The compiler does not require any special action to

prevent the division when y=0.

Page 8: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Unchecked Exceptions

• The programmer can choose to:1. Ignore the exception and hope for the best.2. Catch the exception and implement an action to

recover from the exception.

Note: if the program cannot recover from an exception, the program exits with a fatal exception runtime error.

Page 9: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Unchecked Exception Example

double z;if ( y != 0 ) //prevent 0-division z = x / y;

else// perform some other action

Page 10: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Checked Exception

• Two choices for the programmer:1. Throw the exception to the referencing method

and hope for recovery.2. Catch the exception with a competent exception

handler

Page 11: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Java Keywords for Exception Handling

• An unchecked exception could use the keyword “throw”.

• Checked exceptions can use any of these:– throws– throw– try, catch, & finally

Page 12: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

The Competent Exception Handler

• The competent exception handler uses a combination of these three keywords: try, catch, and finally.

• If the programmer wishes to send the exception to the referencing method, then use the throws keyword.

Page 13: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Syntax for CEH

• In order:1. try-block – one per CEH2. catch-blocks - zero or more per CEH3. finally-block – zero or one per CEH

Page 14: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

1. CEH try-Block

• A CEH must have a try-block• The try-block consists of the try keyword

followed by a body of code.

Page 15: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

2. CEH catch-Blocks

• The try-block is followed by zero or more catch blocks. – Each catch block will perform an action based on

the exception type

Page 16: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

3. CEH finally-Block

• The finally-block is required if there are zero catch blocks.

• The finally-block is optional if there is at least one catch-block.

Page 17: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

CEH

• The CEH can be nested just like other control structures.

Page 18: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

Unchecked Exception with “CEH”

public void withdraw( double amount ) { try // Competent Exception Handler { if ( amount > balance_ ) { // overdraft! if ( OVERDRAFT_PENALTY > balance_ ) balance_ = 0.0; else balance_ -= OVERDRAFT_PENALTY; throw new OverdraftException( "Withdrawal exceeds balance" ); } else { balance_ -= amount; } } catch( OverdraftException e ) { System.out.println( e ); } } // end withdraw method

Page 19: Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course

User Defined Exception Class

// Customized User (Programmer) // Designed Exception Class: public class OverdraftException

extends RuntimeException { public OverdraftException( String reason ) { super ( reason ) ; } }