Docsity

Exception Handling in Java, Slides of Java Programming

Partial preview of the text

Download Exception Handling in Java and more Slides Java Programming in PDF only on Docsity!

Objectives

  • examine Java's exception handling
    1. Exceptions

Contents

1. Motivation

2. Exception Handling (in outline)

3. Many Catch Blocks

4. The Exception Class Hierarchy

5. Throwing an Exception

continued Docsity.com

1. Motivation

• Lots of error checking in code makes the

code harder to understand

– more complex

– more likely that the code will have errors!

• Add error checking to the following C code :

int a[SIZE]; y = ... x = (1.0/a[y]) + (2.0/a[y+1]) + (3.0/a[y+2]);

Some Error Checking (in C)!

if (y >= SIZE) printf(“Array index %d too big\n”, y); else if (a[y] == 0) printf(“First denominator is 0\n”);

if (y+1 >= SIZE) printf(“Array index %d too big\n”, y+1); else if (a[y+1] == 0) printf(“Second denominator is 0\n”);

if (y+2 >= SIZE) printf(“Array index %d too big\n”, y+2); else if (a[y+2] == 0) printf(“Third denominator is 0\n”); :

  1. Exception Handling (in outline)
  • Format of code:

statements; try { code...; } catch (Exception-type e) { code for dealing with e exception } more-statements;

a try block

a catch block

Basic Approach

• The programmer wraps the error-prone

code inside a try block.

• If an exception occurs anywhere in the

code inside the try block, the catch block

is executed immediately

– the block can use information stored in the e

object

continued Docsity.com

  • If the try block finishes successfully

without causing an exception, then

execution skips to the code after the

catch block

  • i.e. to more-statements

Catching Math Errors

int x = 0; int y; : try { y = 1/x; : } catch ( ArithmeticException e) { System.out.println(e); ...; y = 0; }

System.out.println(“y is “ + y);

any Java code

is allowed here

3. Many Catch Blocks

• There can be many catch blocks associated

with a try block

– the choice of which to use is based on matching

the exception object (e) with the argument type of

each catch block

– after a catch handler has finished, execution

continues after all the handlers

Code Format

statements; try { code...; } catch ( NullPointerException e) { code for dealing with a NULL pointer exception } catch ( IOException e) { code for dealing with an IO exception } catch ( MyOwnException e) { code for dealing with a user-defined exception } more-statements;

In More Detail (but not all!)

4.1. Two Exception Categories

• 1. Checked exceptions

– subclasses of Exception

– recovery should be possible for these types of

errors

– your code must include try-catch blocks for these

or the compiler will reject your program

  • e.g. IOException

continued Docsity.com

4.2. Text IO

• IO can generate lots of exceptions, but usually

the program can recover

– e.g. file not found, so look somewhere else

• Most IO methods can produce

java.io.IOException

– a checked exception which your code must handle

with try-catch blocks

Uses checked

1 exceptions

Text Output to a File

• Use the FileWriter class

– open a file

– write to the file

– close the file

• Failure at any point results in an IOException