Testing and Exceptions

Slides:



Advertisements
Similar presentations
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Advertisements

Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Final Review.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Mixing integer and floating point numbers in an arithmetic operation.
Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
For Friday Finish reading chapter 9 WebCT quiz 17.
CS 116 Object Oriented Programming II Lecture 10 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Introduction to programming in java
Chapter 10 – Exception Handling
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Logger, Assert and Invariants
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Introduction to OO Program Design
CSE 501N Fall ’09 17: Exception Handling
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Iteration.
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Chapter 12 Exception Handling and Text IO
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
File I/O ICS 111: Introduction to Computer Science I
Exception Handling and Reading / Writing Files
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
Exception Handling.
Fundamental Error Handling
Exception Handling in Java
Web Design & Development Lecture 7
Exception Handling in Java
Exceptions handling Try, catch blocks Throwing exceptions.
Lecture 11 Objectives Learn what an exception is.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Tutorial Exceptions Handling.
Chapter 12 Exception Handling and Text IO Part 1
Exception Handling Contents
Exceptions.
Tutorial MutliThreading.
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Exceptions.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 15 – Exception Handling
Presentation transcript:

Testing and Exceptions

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;

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

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

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 ); }

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 ); }

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 ); }

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

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 ); }

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?

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

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);

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");