Chapter 12 Exception Handling & Text...

13
Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302 SUMMER 2015 slides created by Rashaad Jones

Transcript of Chapter 12 Exception Handling & Text...

Page 1: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

Chapter 12

Exception Handling

& Text I/O DR. JONES

KENNESAW STATE UNIVERSITY

CS 2302

SUMMER 2015

slides created by Rashaad Jones

Page 2: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

Contents

Overview

Exception

Exception handling

What they are

Error types

Try-Throw-Catch Exceptions

Getting Information from

Exceptions

finally

General Guidelines

File

File Class

PrintWriter

Scanner

Page 3: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

Exception Handling

Exceptions are thrown from a method

Directly by using a throw statement in a try block

Invoking a method that may throw an exception

Exceptions have messages

Help users understand what happened

Help users understand how to resolve

Help programmers and troubleshooters understand how to diagnose

Specificity is important

Page 4: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

Try block -- Example

Code snippet

System.out.println("Enter a double:");

double a = scan.nextDouble();

Output if number is not entered

ÏException in thread "main" java.util.InputMismatchException

Code snippet

try

{

System.out.println("Enter a double:");

double a = scan.nextDouble();

}

catch (Exception e)

{

System.out.println("You did not enter a number");

}

Output if number is not entered

You did not enter a number

WITHOUT TRY WITH TRY

Page 5: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

throw example

Code snippet

public static void method(double a, double b)

{

if(b != 0)

{

System.out.println(a/b);

}

else

System.out.println(“Divisor is not a number”);

}

Output if b is not a number

Divisor is not a number

Code snippet

public static void method(double a, double b) throws Exception

{

if(b != 0)

{

System.out.println(a/b);

}

else

throw new Exception(“Divisor is not a number”);

}

Output if b is not a number

You did not enter a number

WITHOUT THROW WITH THROW

Throw -- Example

Page 6: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

Catching errors -- Example

public static void methodwithException(double

a, double b) throws Exception

{

if(b != 0)

{

System.out.println(a/b);

}

else

throw new Exception(“Divisor is not a

number”);

}

public static void methodCaller()

{

java.util.Scanner scan = new Scanner(System.in);

double a = scan.nextDouble();

double b = scan.nextDouble();

try

{

methodwithException(a, b);

}

catch (Exception e)

{

System.out.println(“ERROR” + e.getMessage());

}

}

METHOD WITH

EXCEPTION

METHOD THAT

CATCHES EXCEPTION

Page 7: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

Getting information from errors -- Example

public static void methodwithException(double

a, double b) throws Exception

{

if(b != 0)

{

System.out.println(a/b);

}

else

throw new Exception(“Divisor is not a

number”);

}

public static void methodCaller()

{

java.util.Scanner scan = new Scanner(System.in);

double a = scan.nextDouble();

double b = scan.nextDouble();

try

{

methodwithException(a, b);

}

catch (Exception e)

{

System.out.println(“ERROR” + e.getMessage());

}

}

METHOD WITH

EXCEPTION

METHOD THAT

CATCHES EXCEPTION

Page 8: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

Error Types

System errors

• Provide user message prior to closing

• Represented by the Error class

Run-time exceptions

• Programming errors

• Represented by the RuntimeException class

Exception errors

• Provide mechanisms to continue running program

• Represented by the Exception class

FATAL

NON-

FATAL

Page 9: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

public void openFile()

{

FileReader reader = null;

try

{

reader = new FileReader("someFile");

int i=0;

while(i != -1)

{

i = reader.read();

System.out.println((char) i );

}

}

catch (IOException ioe)

{

System.out.println(“ERROR:” + ioe.getMessage());

}

finally

{

if(reader != null)

{

try

{

reader.close();

}

catch (IOException e)

{

//nothing needs to be done

//because reader is null

}

}

}

Page 10: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

General Guidelines

main method catch all exceptions

try-catch-finally block

all other methods throws exceptions

use Custom Exceptions

Class that extends a specific Exception or Error class

Specific message that helps to diagnose and/or resolve

Bad error message: “Error encountered in code”

Good error message: “Code: 6215. Error detected in line 592 of code. Cannot parse the file, StudentNames.txt because information is missing in row 12 of file. Program will not be able to continue analysis. Please contact support and provide this message.”

Page 11: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

File Class

Contains behaviors and properties for obtaining of a file/directory

and for renaming and deleting a file/directory

Examples

Directory: File f = new File(“book”);

File: File f = new File(“book.dat”);

Reminders

Do not use absolute file names in your program

Page 12: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

PrintWriter Class

java.io.PrinterWriter output = null;

try

{

output = new java.io.PrintWriter(new

File(“scores.txt”);

output.print(“John Doe”);

output.println(90);

output.print(“Jane Doe”);

output.println(85);

}

catch (Exception e)

{

System.out.println(“Error with writing

to file”);

}

finally

{

//close the file (MUST BE DONE)

output.close();

}

Page 13: Chapter 12 Exception Handling & Text I/Oksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Chapter 12 Exception Handling & Text I/O DR. JONES KENNESAW STATE UNIVERSITY CS 2302

Scanner Class

Scanner input = new Scanner(new File(“scores.txt”));

try

{

while(input.hasNext())

{

String firstName = input.next();

String lastName = input.next();

int score = input.nextInt();

System.out.println(firstName + “ “ + lastName + “ “ + score);

}

}

catch (Exception e)

{

System.out.println(“Error “ + e.getMessage();

}

finally

{

//close the file

input.close();

}