ITP © Ron Poet Lecture 9 1 Exceptions. ITP © Ron Poet Lecture 9 2 The Standard Way for Object to Return Work  Object 1 gives object 2 some work.  It.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
CS102--Object Oriented Programming
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions CIS 304 Intermediate Java Programming for Business.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
ITP © Ron Poet Lecture 2 1 Mental Models. ITP © Ron Poet Lecture 2 2 Mental Models  A mental model is a way of making sense of something we experience.
1 Web Based Programming Section 6 James King 12 August 2003.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Chapter 12: Exception Handling
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
ITP © Ron Poet Lecture 3 1 Comments. ITP © Ron Poet Lecture 3 2 Legibility  It is important that programs are easy to read.  It is easier to find bugs.
Java Programming: Guided Learning with Early Objects
Exceptions Handling Exceptionally Sticky Problems.
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 8-Exception Handling/ Robust Programming.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Handling Exceptionally Sticky Problems
Incrementing ITP © Ron Poet Lecture 8.
Exceptions C++ Interlude 3
Exceptions with Functions
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
Part B – Structured Exception Handling
Handling Exceptionally Sticky Problems
CMSC 202 Exceptions.
Presentation transcript:

ITP © Ron Poet Lecture 9 1 Exceptions

ITP © Ron Poet Lecture 9 2 The Standard Way for Object to Return Work  Object 1 gives object 2 some work.  It calls a method of object 2.  It waits until object 2 has finished.  Object 2 may well give some of the work to object 3, and so on, down to object 10.  We have a long chain of objects waiting for the one at the end of the chain to finish.

ITP © Ron Poet Lecture 9 3 Shorting Out The Chain  An exception is a way of cutting out the middle men.  Object 10 can get back to object 1 directly.  Bypassing all the middle men.  Used in emergencies  Object 10 had detected a serious problem.  Trying readDouble when the input contains letters, for example.

ITP © Ron Poet Lecture 9 4 Also Convenient  An exception is also a convenient way of returning from a method in some cases.  When something unexpected happens.  Trying to read from a file and reaching the end of file.  An end of file exception is thrown.  Do not overuse!

ITP © Ron Poet Lecture 9 5 Mental Model of an Exception  Object 10 writes down a special message on a piece of paper  folds it into the shape of a plane  and throws it towards object 9.  Object 9 can catch it or let it sail over his head.  If this happens, the plain flies towards object 8.  It is a powered and guided paper aeroplane.

ITP © Ron Poet Lecture 9 6 Mental Model (2)  This process continues until an object catches it or it reaches the main object.  If main does not catch it then we have a runtime error  Unhandled exception.

ITP © Ron Poet Lecture 9 7 The Special Message  The special message is also an object  An exception object.  It will contain information on why the exception object was thrown.  It can be used in the catch block by the object that caught it.

ITP © Ron Poet Lecture 9 8 Why Worry About Exceptions  If we use library code that might throw an exception, we cannot avoid exceptions.  We cannot stop an exception from being thrown.  Part of our code must be prepared to catch the exception.  The compiler will not let us avoid this.

ITP © Ron Poet Lecture 9 9 Simple Exception Catching  The FormatIOX package is a variant of FormatIO that uses exceptions.  Example, catching the EndOfFileException thrown by FileIn and StringIn.  Any method that might throw this exception must be inside a try block.  The code to deal with the exception if it thrown must be inside a catch block.

ITP © Ron Poet Lecture 9 10 Exception Example try { line = fin.readLine(); } catch (EndOfFileException x) { System.err.println(“Unexpected end of file”); }

ITP © Ron Poet Lecture 9 11 Example Explained  The try block is the compound statement after the word try.  The readLine method might throw an EndOfFileException exception.  Therefore it must be inside the try block.  The code to handle the exception is inside the catch(EndOfFileException x) block.  In this case it ignores the exception object x.

ITP © Ron Poet Lecture 9 12 Two Pathways In The Code  If no exception is thrown:  Our code executes all the instructions in the try block.  The catch block is ignored.  If an exception is thrown:  Our code only does those bits of the try block that happen before the exception.  It then jumps straight to the catch block.

ITP © Ron Poet Lecture 9 13 Catching More than One Exception  A NumberFormatException, number format exception might also be thrown.  Trying to read a number when the input contains “Fred”, for example.  We can have several catch blocks, one after another.

ITP © Ron Poet Lecture 9 14 Catching Two Exceptions try { line = fin.readLine(); } catch (EndOfFileException x) { System.err.println(“Unexpected end of file”); } catch (NumberFormatException x) { System.err.println(“Number Format Error”); }

ITP © Ron Poet Lecture 9 15 Scope of a Variable

ITP © Ron Poet Lecture 9 16 Scope is Visibility  The scope of a variable is the part of the program where it can be used.  Its scope is the compound statement where it is defined.  A compound statement is also called a block.  The scope includes all inner blocks.

ITP © Ron Poet Lecture 9 17 Scope Example String line = ""; FileIn fin = new FileIn("..\\radius.txt"); try { line = fin.readLine(); } catch( EndOfFileException x){} StringIn sin = new StringIn(line);

ITP © Ron Poet Lecture 9 18 Scope Example Explained  The variable line is declared in the main block.  It can be used throughout the code shown.  Including the inner try block.  fin is also defined in the main block.  It can be used anywhere after it is defined.

ITP © Ron Poet Lecture 9 19 Example That Does Not Work  If we define the variable line inside the try block.  We cannot use it outside that block.  It no longer exists by the time we reach the sin definition.  The following code will generate a syntax error.

ITP © Ron Poet Lecture 9 20 Wrong Scope FileIn fin = new FileIn("..\\radius.txt"); try { String line = fin.readLine(); } catch( EndOfFileException x){} StringIn sin = new StringIn(line);

ITP © Ron Poet Lecture 9 21 Keeping The Compiler Happy

ITP © Ron Poet Lecture 9 22 Combining Scope and Exceptions  Here is an initial version of code to read a word from fin. String word = fin.readWord(); con.print(word);  This will not work because we have ignored the EndOfFileException exception.  We must catch EndOfFileException.

ITP © Ron Poet Lecture 9 23 Catching The Exception try { String word = fin.readWord(); } catch(EndOfFileException x) {} con.print(word);  This will not work because word is out of scope by the time we reach con.print.  We must define word before the try block.

ITP © Ron Poet Lecture 9 24 Uninitialised Variable String word; try { String word = fin.readWord(); } catch(EndOfFileException x) {} con.print(word);  The compiler now complains about word being uninitialised.

ITP © Ron Poet Lecture 9 25 Two Paths Through try-catch  word is uninitialised because there are 2 routes through a try-catch.  We could go right through the try block.  word will get a value.  We could exit the try block early, jumping to the catch.  word does not get a value.  We must initialise word before the try.

ITP © Ron Poet Lecture 9 26 Initialised Variable String word = ""; try { String word = fin.readWord(); } catch(EndOfFileException x) {} con.print(word);  Now the compiler is happy!