Unit 10 : exception handling and file I/O Today : more on - - PowerPoint PPT Presentation

unit 10 exception handling and file i o
SMART_READER_LITE
LIVE PREVIEW

Unit 10 : exception handling and file I/O Today : more on - - PowerPoint PPT Presentation

Unit 10 : exception handling and file I/O Today : more on exceptions; introduction to files (maybe?) 1 not in notes What happens if the user the following input during program execution? 5 a import java.util.Scanner; public class


slide-1
SLIDE 1

not in notes

1

Unit 10: exception handling and file I/O

Today: more on exceptions; introduction to files (maybe?)

slide-2
SLIDE 2

not in notes

2

What happens if the user the following input during program execution? 5 a

import java.util.Scanner; public class QuotientShort{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two integers: "); int n1 = input.nextInt(); int n2 = input.nextInt(); System.out.println(n1 + " / " + n2 + " is " + (n1 / n2)); System.out.println(n1 + " * " + n2 + " is " + (n1 * n2)); } }

slide-3
SLIDE 3

not in notes

3

answer

$$ java QuotientShort Enter two integers: 5 a Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:818) at java.util.Scanner.next(Scanner.java:1420) at java.util.Scanner.nextInt(Scanner.java:2029) at java.util.Scanner.nextInt(Scanner.java:1989) at QuotientShort.main(QuotientShort.java:9)

slide-4
SLIDE 4

not in notes

4

What happens if the user the following input? 5 a

import java.util.Scanner; public class QuotientShortException{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two integers: "); int n1 = 0, n2 = 0; try { n1 = input.nextInt(); } catch(ArithmeticException ex){ System.out.println("Exception: Bad input"); } try { n2 = input.nextInt(); } catch(ArithmeticException ex){ System.out.println("Exception: Bad input"); } System.out.println(n1 + " / " + n2 + " is " + (n1 / n2)); System.out.println(n1 + " * " + n2 + " is " + (n1 * n2)); } }

slide-5
SLIDE 5

not in notes

5

answer

$$ java QuotientShort Enter two integers: 5 a Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:818) at java.util.Scanner.next(Scanner.java:1420) at java.util.Scanner.nextInt(Scanner.java:2029) at java.util.Scanner.nextInt(Scanner.java:1989) at QuotientShort.main(QuotientShort.java:9)

slide-6
SLIDE 6

not in notes

6

What happens if the user the following input? 5 a

import java.util.Scanner; public class QuotientShortException2{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two integers: "); int n1 = 1, n2 = 1; try { n1 = input.nextInt(); } catch(java.util.InputMismatchException ex){ System.out.println("Inception: Bad input"); } try { n2 = input.nextInt(); } catch(java.util.InputMismatchException ex){ System.out.println("Exception: Bad input"); } System.out.println(n1 + " / " + n2 + " is " + (n1 / n2)); System.out.println(n1 + " * " + n2 + " is " + (n1 * n2)); } }

slide-7
SLIDE 7

not in notes

7

answer

Enter two integers: 5 a Exception: Bad input 5 / 1 is 5 5 * 1 is 5

slide-8
SLIDE 8

not in notes

8

Hypothetical scenario:

For Assignment 568 for COMP-202, you must write a program that asks the user for two integers and prints out the result of the famous Ackermann function (see Wikipedia). You are provided with the documentation (but not the source code) of a class called MysteryClass that contains a method called ackermann(). You have written the requested program. It compiles without error. Now you must test it for run-time errors. Here's your program:

import java.util.Scanner; public class MysteryError{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two integers: "); int n1 = input.nextInt(); int n2 = input.nextInt(); int result = MysteryClass.ackermann(n1,n2); System.out.println("The result is " + result); } }

slide-9
SLIDE 9

not in notes

9

Good programming practice: separate error identification from error handling

Suppose m1 is a method that calls another method m2:

  • m1 does no try to figure out which errors can possibly

come up due to calling m2.

  • Instead, m2 passes on error information its caller, m1.
  • The caller, m1, handles any error information that it is

provided with by m2.

  • This is why Java has exceptions: exceptions are

automatically passed from a method to its callers.

slide-10
SLIDE 10

not in notes

10

Why Exceptions Are Useful

  • Since we don't know what is in the body of the

ackermann() method, we have no way to figure

  • ut which integers trigger the exception it

produces

  • The only way to eliminate the runtime error is to

use a try/catch block to handle the exception (or remove the call to ackermann() from our program).

slide-11
SLIDE 11

not in notes

11

Why Exceptions Are Useful

  • This is a very good example of why “separating

error identification from error handling” is good

  • practice. The error is identified in ackermann()

and we are able handle in main() without having to understand anything about the cause of th error

  • r the details about ackermann().
slide-12
SLIDE 12

not in notes

12

There are two kinds of exceptions: unchecked vs checked

All the exceptions in

  • ur examples so far

are unchecked

slide-13
SLIDE 13

not in notes

13

Every method in your programs must declare all the checked exceptions it encounters.

public static void main(String[] args) throws IOException { // some code here that causes an IOException // more details about this next week }

slide-14
SLIDE 14

not in notes

14

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at VetClinicTest.main(VetClinicTest.java:8)

Say you encounter an exception like this...

To find out whether it is checked or unchecked, look it up in the Java API documentation

(http://download.oracle.com/javase/6/docs/api/)

slide-15
SLIDE 15

not in notes

15

slide-16
SLIDE 16

not in notes

16

Introduction to I/O

  • Programs can read information from different

locations such as

  • “Standard input”
  • Files on your hard drive
  • Programs can write information to different

locations such as

  • “Standard output”
  • Files on your hard drive
slide-17
SLIDE 17

COMP 202 – Introduction to Computing 1 17

Programs read/write information through I/O Streams

  • A stream is a sequence of bytes that flow from a

source to a destination

  • In a program, we read information from an input

stream and write information to an output stream

  • A program can manage multiple streams at a time
  • The java.io package contains many classes that

allow us to define various streams with specific characteristics

slide-18
SLIDE 18

COMP 202 – Introduction to Computing 1 18

I/O Stream Categories

  • The classes in the I/O package divide input and output

streams into other categories

  • An I/O stream is either a

– character stream, which deals with text data – byte stream, which deal with byte (binary) data

slide-19
SLIDE 19

COMP 202 – Introduction to Computing 1 19

Standard I/O

  • There are three standard I/O streams
  • The System class contains three object reference

variables (in, out, err)

– declared public and static (can be accessed via class name).

  • System.in

– standard input (typically keyboard) – we give System.in as input to Scanner constructor to read from keyboard

  • System.out

– standard output (typically a window on screen) – println is method of out output stream, thus to print to standard output we call System.out.println

slide-20
SLIDE 20

COMP 202 – Introduction to Computing 1 20

The Standard Input Stream

  • We’ve used the standard input stream to create a

Scanner object to process input read interactively from the user:

Scanner scan = new Scanner (System.in);

  • The Scanner object converts bytes from the

stream into characters, and provides various methods to access those characters (by line, by word, by type, etc.)

slide-21
SLIDE 21

not in notes

21

Summary/Goals

  • You should be able to explain why exceptions are

useful and part of good programming practice.

  • You should remember that exceptions are objects

and that they can be divided into two categories: checked and unchecked.

  • Checked and unchecked exceptions require different

treatment.