Presentation is loading. Please wait.

Presentation is loading. Please wait.

Testing and Exceptions

Similar presentations


Presentation on theme: "Testing and Exceptions"— Presentation transcript:

1 Testing and Exceptions

2 Exception Abnormal event occurring during program execution Examples
Manipulate nonexistent files File file = new File(s); Scanner fileIn = new Scanner(file); Improper array subscripting int[] a = new int[3]; a[4] = 1000; Improper arithmetic operations a[2] = 1000 / 0;

3 Java treatment of an exception
If exception occurs and a handler is in effect Flow of control is transferred to the handler After handler completes flow of control continues with the statement following the handler If exception occurs and there is no handler for it The program terminates

4 Task Prompt and extract the name of a file
From that file, two integer values are to be extracted Compute and display the quotient of the values

5 Implementation public static void main(String[] args) throws IOException { Scanner stdin = new Scanner(System.in); System.out.print("Filename: "); String s = stdin.nextLine(); File file = new File(s); Scanner fileIn = new Scanner(file); int a = fileIn.nextInt(); int b = fileIn.nextInt(); System.out.println( a / b ); }

6 What can go wrong? public static void main(String[] args) throws IOException { Scanner stdin = new Scanner(System.in); System.out.print("Filename: "); String s = stdin.nextLine(); File file = new File(s); Scanner fileIn = new Scanner(file); int a = fileIn.nextInt(); int b = fileIn.nextInt(); System.out.println( a / b ); }

7 How can we deal with the problems?
public static void main(String[] args) throws IOException { Scanner stdin = new Scanner(System.in); System.out.print("Filename: "); String s = stdin.nextLine(); File File = new File(s); Scanner fileIn = new Scanner(file);( int a = fileIn.nextInt()); int b = fileIn.nextInt()); System.out.println( a / b ); }

8 Exception handlers Code that might generate an exception is put in a try block If there is no exception, then the handlers are ignored For each potential exception type there is a catch handler When handler finishes the program continues with statement after the handlers try { Code that might throw exceptions of types E or F } catch (E e) { Handle exception e catch (F f) { Handle exception f More code

9 Introduce try-catch blocks
public static void main(String[] args) throws IOException { Scanner stdin = new Scanner(System.in); System.out.print("Filename: "); String s = stdin.nextLine(); File file = new File(s); Scanner fileIn = new Scanner(file); int a = fileIn.nextInt(); int b = fileIn.nextInt()); System.out.println( a / b ); }

10 Setting up the file stream processing
Scanner stdin = new Scanner(System.in); System.out.print("Filename: "); String s = stdin.nextLine(); // set up file stream for processing Scanner fileIn = null; try { File file = new File(s); fileIn = new Scanner(file); } catch (FileNotFoundException e) { System.err.println(s + “: Cannot be opened for reading"); System.exit(0); How come the main() throws expression did not indicate it could throw a FileNotFoundException?

11 Run time exceptions Java designers realized
Runtime exceptions can occur throughout a program Cost of implementing handlers for runtime exceptions typically exceeds the expected benefit Java makes it optional for a method to catch them or to specify that it throws them However, if a program does not handle its runtime exceptions it is terminated when one occurs

12 Getting the inputs try { int a = fileIn.nextInt();
int b = fileIn.nextInt(); System.out.println( a / b ); } catch (InputMismatchException e) { System.err.println(s + ": contains nonnumeric inputs"); System.exit(0);

13 Converting the inputs try { int a = fileIn.nextInt();
int b = fileIn.nextInt(); System.out.println( a / b ); } catch (InputMismatchException e) { System.err.println(s + ": contains non-integral inputs"); System.exit(0); catch (NoSuchElementException e) { System.err.println(s + ": does not contain two inputs"); catch (ArithmeticException e) { System.err.println(s + ": unexpected 0 input value");


Download ppt "Testing and Exceptions"

Similar presentations


Ads by Google