Geoff Holmes Week 5 problems Handling Throwing Catching Multiple exceptions Finally clause Examples Exception Handling.

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
COMP 121 Week 5: Exceptions and Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
COMP201 Java Programming Topic 6: Exceptions Reading: Chapter 11.
Exceptions and Exception Handling Carl Alphonce CSE116.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
COMP201 Java Programming Topic 7: Exceptions Reading: Chapter 11.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Slides Credit Umair Javed LUMS Web Application Development.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
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.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
© 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
CS 61B Data Structures and Programming Methodology July 7, 2008 David Sun.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Java Exceptions a quick review….
CS102 – Exceptions David Davenport Latest: May 2015
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Exception Handling and Reading / Writing Files
Java Exceptions Dan Fleck CS211.
Exceptions 10-May-19.
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 Handling.
Presentation transcript:

Geoff Holmes Week 5 problems Handling Throwing Catching Multiple exceptions Finally clause Examples Exception Handling

Department of Computer Science2 Week 5 Problems Write a program to read a file of filenames representing images (*.jpg). Store the filenames in an array and iteratively display the contents of each file (see Section 18.4). Exercise 16.1 (Exception handling) Exercise 14.2 – read a file and count the number of lines, words and characters (covers Utility classes) Exercise 19.3 – collection classes, will start to do either Tuesday or Friday.

Department of Computer Science3 Handling Errors In exceptional circumstances (eg bad data input) we use Java’s exception handling system. Where do errors come from?  User input errors  Device errors (printer turned off)  Physical limits (disk full, out of memory)  Code errors – array indexes, pop an empty stack. Normal error handling is to return (-1) but this doesn’t always work. Also, it isn’t OOP to return a number to mark an error state.

Department of Computer Science4 Motivation Exceptions are events that disrupt normal program flow; e.g. processing a file:  Ask the user for filename  Open file  Read contents  Do something with contents  Close file Lots of potential problems (file not found, not readable, …) Java supports strong distinction between normal and abnormal program flow

Department of Computer Science5 Exception hierarchy Throwable  Error  LinkageError  VirtualMachineError  …  Exception  IOException  RuntimeException ClassCastException …  … Every exception is an instance of a subclass of Throwable Programmers are not forced to deal with Error and RuntimeException cases (too common)

Department of Computer Science6 Throwing your own Suppose you have a problem with premature EOF when reading data. You know it is an IOException and find EOFException: String readData(BufferedReader in) throws EOFException { While (…) { If (n < len) throw new EOFException( ); … }

Department of Computer Science7 Can give EOFException a string String problem = “Should be “ + len + “ but is “ + n; throw new EOFException(problem); So for exisiting exceptions we:  Find the appropriate class  Make an object of that class  Throw it

Department of Computer Science8 No exception fits? class FileFormatException extends IOException { public FileFormatException( ) { } public FileFormatException(String problem) { super(problem); } String readData(BufferedReader in) throws FileFormatException { While (…) { If (n < len) throw new FileFormatException( ); … }

Department of Computer Science9 Catching Exceptions Use a try/catch block to catch exceptions: try {code more code } catch (ExceptionType e) {handler for this type }

Department of Computer Science10 What happens? If any code in the try block throws an exception of the class specified in the catch block then the program skips the remaining code and executes the handler code. If none of the code throws an exception then the catch block is skipped.

Department of Computer Science11 Example 1 – ExceptionTest Try to pop an empty stack!

Department of Computer Science12 Rules for passing or treating Catch those exceptions that you know how to handle Propagate those that you do not!

Department of Computer Science13 Catching any exception – ExceptionMethods catch (Exception e) { System.out.println(“caught an exception”); } To get better info on the error we can use: 1. e.getMessage( ) 2. e.toString( ) 3. e.printStackTrace( )

Department of Computer Science14 Multiple Exceptions Try { code that might contain many exceptions } catch (MalformedURLException e1) { code to handle bad URLs } catch (UnknownHostException e2) { code to handle unknown hosts } catch (IOEXception e3) { code to handle all other IO problems }

Department of Computer Science15 Book example try { File fd1 = new File(name); File fd2 = new File(name); processFile(fd1,fd2); fd1.close(); fd2.close(); } catch (FileNotFoundException e) { System.err.println(“Cannot find file “ + e); } catch (FileOpenFailed e) { System.err.println(“Cannot open file “ + e); }

Department of Computer Science16 Finally clause Optional last clause in a try/catch block Will always be executed, no matter what exceptions have been thrown and handled: try { throw new EOFException(); } catch (EOFException e) { e.printStackTrace(); throw new ClassCastException(); } finally { System.out.println(“Finally”); } Will even execute when “return” is used!

Department of Computer Science17 Final note Graphics g = image.getGraphics( ); try { code that might throw exceptions } catch (IOException e) { done = true; } finally { g.dispose( ); } 1. Suppose no exceptions thrown 2. Exception thrown of type IOException 3. Exception thrown not of type IOException