Advanced Flow of Control - Introduction -ExceptionsException HandlingSlide 4The try StatementException PropagationSlide 7The throw StatementThe finally ClauseThreadsSlide 11Shared DataSlide 13SynchronizationControlling Threads1Advanced Flow of Control - Introduction - Two additional mechanisms for controlling process execution are exceptions and threadsChapter 14 focuses on:exception processingcatching and handling exceptionscreating new exceptionsseparate process threadssynchronizing threads2ExceptionsAn exception is an object that describes an unusual or erroneous situationExceptions are thrown by a program, and may be caught and handled by another part of the programA program can therefore be separated into a normal execution flow and an exception execution flowAn error is also represented as an object in Java, but usually represents a unrecoverable situation and should not be caught3Exception HandlingA program can deal with an exception in one of three ways:ignore ithandle it where it occurshandle it in another place in the programThe manner in which an exception is processed is an important design consideration4Exception HandlingIf an exception is ignored by the program, the program will terminate and produce an appropriate messageThe message includes a call stack trace that indicates on which line the exception occurredThe call stack trace also shows the method call trail that lead to the execution of the offending lineSee Zero.java5The try StatementTo process an exception when it occurs, the line that throws the exception is executed within a try blockA try block is followed by one or more catch clauses, which contain code to process an exceptionEach catch clause has an associated exception typeWhen an exception occurs, processing continues at the first catch clause that matches the exception typeSee Adding.java6Exception PropagationIf it is not appropriate to handle the exception where it occurs, it can be handled at a higher levelExceptions propagate up through the method calling hierarchy until they are caught and handled or until they reach the outermost levelA try block that contains a call to a method in which an exception is thrown can be used to catch that exceptionSee Propagation_Demo.java7ExceptionsAn exception is either checked or uncheckedA checked exception can only be thrown within a try block or within a method that is designated to throw that exceptionThe compiler will complain if a checked exception is not handled appropriatelyAn unchecked exception does not require explicit handling, though it could be processed that way8The throw StatementA programmer can define an exception by extending the appropriate classExceptions 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 thrown9The finally ClauseA try statement can have an optional clause designated by the reserved word finallyIf no exception is generated, the statements in the finally clause are executed after the statements in the try block completeAlso, if an exception is generated, the statements in the finally clause are executed after the statements in the appropriate catch clause complete10ThreadsProcessing 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 processingA thread is one sequential flow of execution that occurs at the same time another sequential flow of execution is processing the same programThey are not necessarily executing the same statements at the same time11ThreadsA thread can be created by deriving a new thread from the Thread classThe run method of the thread defines the concurrent activity, but the start method is used to begin the separate thread process See Simultaneous.javaA thread can also be created by defining a class that implements the Runnable interfaceBy implementing the interface, the thread class can be derived from a class other than Thread12Shared DataPotential problems arise when multiple threads share dataSpecific code of a thread may execute at any point relative to the processing of another threadIf that code updates or references the shared data, unintended processing sequences can occur that result in incorrect results13Shared DataConsider two withdrawals from the same bank account at the same timetask: withdraw 300Is amount <= balanceYESbalance -= 300balance531231-69task: withdraw 300Is amount <= balanceYESbalance -= 30014SynchronizationMultiple threads of control can be made safe if areas of code that use shared data are synchronizedWhen a set of code is synchronized, then only one thread can be using that code at a timeThe other threads must wait until the first thread is completeThis is an implementation of a synchronization mechanism called a monitorSee ATM_Accounts.java15Controlling ThreadsThread processing can be temporarily suspended, then later resumed, using methods from the Thread classA thread can also be put to sleep for a specific amount of timeThese mechanisms can be quite helpful in certain situations, like controlling animationsSee
or
We will never post anything without your permission.
Don't have an account? Sign up