Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
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.
COMP 121 Week 5: Exceptions and Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
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.
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.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Java Programming Transparency No. 1-1 Java Exception Handling Cheng-Chia Chen.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Exceptions. 2 Objectives Introduce C# exception handling –library exception types –custom exceptions Describe keywords used for exception handling –try.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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.
Preventing and Correcting Errors
1 Advanced Flow of Control - Introduction - zTwo additional mechanisms for controlling process execution are exceptions and threads zChapter 14 focuses.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
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.
Slides Credit Umair Javed LUMS Web Application Development.
10-1 Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and may be caught and handled.
1 Lecture 11(chap 14) Exception Handling & Thread Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
© 2004 Pearson Addison-Wesley. All rights reserved November 30, 2007 Exceptions ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Chapter 10 Exceptions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved December 5, 2007 I/O Exceptions & Working with Files ComS 207: Programming I (in Java) Iowa State University,
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
CSE 1201 Object Oriented Programming
16 Exception Handling.
10 Exceptions Software Solutions Lewis & Loftus java 5TH EDITION
EE422C Software Implementation II
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling Chapter 9 Edited by JJ.
Exceptions April 19, 2006 ComS 207: Programming I (in Java)
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
I/O Exceptions & Working with Files
Exception Handling.
Presentation transcript:

Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control -- Introduction Two additional mechanisms for controlling process execution are exceptions and threads Chapter 14 focuses on: –exception processing –catching and handling exceptions –creating new exceptions –separate process threads –synchronizing threads

Java Software Solutions Lewis and Loftus Chapter 14 2 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and may be caught and handled by another part of the program A program can therefore be separated into a normal execution flow and an exception execution flow An error is also represented as an object in Java, but usually represents a unrecoverable situation and should not be caught

Java Software Solutions Lewis and Loftus Chapter 14 3 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Exception Handling A program can deal with an exception in one of three ways: –ignore it –handle it where it occurs –handle it an another place in the program The manner in which an exception is processed is an important design consideration

Java Software Solutions Lewis and Loftus Chapter 14 4 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Exception Handling If an exception is ignored by the program, the program will terminate and produce an appropriate message The message includes a call stack trace that indicates on which line the exception occurred The call stack trace also shows the method call trail that lead to the execution of the offending line See Zero.java

Java Software Solutions Lewis and Loftus Chapter 14 5 Copyright 1997 by John Lewis and William Loftus. All rights reserved. The try Statement To process an exception when it occurs, the line that throws the exception is executed within a try block A try block is followed by one or more catch clauses, which contain code to process an exception Each catch clause has an associated exception type When an exception occurs, processing continues at the first catch clause that matches the exception type See Adding.java

Java Software Solutions Lewis and Loftus Chapter 14 6 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Exception Propagation If it is not appropriate to handle the exception where it occurs, it can be handled at a higher level Exceptions propagate up through the method calling hierarchy until they are caught and handled or until they reach the outermost level A try block that contains a call to a method in which an exception is thrown can be used to catch that exception See Propagation_Demo.java

Java Software Solutions Lewis and Loftus Chapter 14 7 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Exceptions An exception is either checked or unchecked A checked exception can only be thrown within a try block or within a method that is designated to throw that exception The compiler will complain if a checked exception is not handled appropriately An unchecked exception does not require explicit handling, though it could be processed that way

Java Software Solutions Lewis and Loftus Chapter 14 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. The throw Statement A programmer can define an exception by extending the appropriate class Exceptions are thrown using the throw statement: throw exception-object ; See Throw_Demo.java Usually a throw statement is nested inside an if statement that evaluates the condition to see if the exception should be thrown

Java Software Solutions Lewis and Loftus Chapter 14 9 Copyright 1997 by John Lewis and William Loftus. All rights reserved. The finally Clause A try statement can have an optional clause designated by the reserved word finally If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete Also, if an exception is generated, the statements in the finally clause are executed after the statements in the appropriate catch clause complete

Java Software Solutions Lewis and Loftus Chapter Copyright 1997 by John Lewis and William Loftus. All rights reserved. Threads Processing can be broken into several separate threads of control which execute at the same time "At the same time" could mean true parallelism or simply interlaced concurrent processing A thread is one sequential flow of execution that occurs at the same time another sequential flow of execution is processing the same program They are not necessarily executing the same statements at the same time

Java Software Solutions Lewis and Loftus Chapter Copyright 1997 by John Lewis and William Loftus. All rights reserved. Threads A thread can be created by deriving a new thread from the Thread class The run method of the thread defines the concurrent activity, but the start method is used to begin the separate thread process See Simultaneous.java A thread can also be created by defining a class that implements the Runnable interface By implementing the interface, the thread class can be derived from a class other than Thread

Java Software Solutions Lewis and Loftus Chapter Copyright 1997 by John Lewis and William Loftus. All rights reserved. Shared Data Potential problems arise when multiple threads share data Specific code of a thread may execute at any point relative to the processing of another thread If that code updates or references the shared data, unintended processing sequences can occur that result in incorrect results

Java Software Solutions Lewis and Loftus Chapter Copyright 1997 by John Lewis and William Loftus. All rights reserved. Shared Data Consider two withdrawals from the same bank account at the same time task: withdraw 300 Is amount <= balance YES balance -= 300 balance task: withdraw 300 Is amount <= balance YES balance -= 300

Java Software Solutions Lewis and Loftus Chapter Copyright 1997 by John Lewis and William Loftus. All rights reserved. Synchronization Multiple threads of control can be made safe if areas of code that use shared data are synchronized When a set of code is synchronized, then only one thread can be using that code at a time The other threads must wait until the first thread is complete This is an implementation of a synchronization mechanism called a monitor See ATM_Accounts.java

Java Software Solutions Lewis and Loftus Chapter Copyright 1997 by John Lewis and William Loftus. All rights reserved. Controlling Threads Thread processing can be temporarily suspended, then later resumed, using methods from the Thread class A thread can also be put to sleep for a specific amount of time These mechanisms can be quite helpful in certain situations, like controlling animations See Bouncing_Ball2.java