Advanced Flow of Control I) Main concepts A) exceptions (helps handle "problems") (Savitch: Chapter 8) B) threads (helps allow simultaneous tasks) (Savitch???) II) "problems" with programs A) Meaning -- when programmer or user does something illegal B) Exception 1. object that defines an unusal or erroneous situation 2. the program or runtime environment throws an exception 3. something catches the exception 4. then the "something" handles the exception C) Error 1. like exception, but the program can't recover 2. usually nothing catches an error D) Throwable class 1. superclass for Exception and Error classes 2. see (pg 526, API) for many exceptions and errors E) Why bother? 1. helps debug 2. helps prevent crashes 3. makes more code easier to read/understand by moving error checking out of "important" code III) Exceptions A) Approaches 1. not handle 2. handle where it occurs 3. handle elsewhere in program B) Not Handle 1. program aborts 2. program produces a message that tries to explain why ex) divide by zero /****************************************************************************/ Example) // except1.java // not handle exceptions public class except1 { public static void main(String args[]) { int a = 1; int b = 0; System.out.println(a/b); // can't divide by zero } } Output: java.lang.ArithmeticException: / by zero -> reason for exception at except1.main(Compiled Code) -> call stack trace /****************************************************************************/ C) Handle exception when/where it occurs 1. use try statement: try { } 2. Java will "try" statements that might issue an exception 3. if issue, or THROW, an exception then use catch clauses to handle different possibilities 4. use series of catch ( ) { statements } to handle different kinds of exceptions a. try throws exceptions -- catch catches them b. control goes to 1st catch clause whose exception_class matches the exception c. each catch clause called an "exception handler" d. see also finally 5. if no exceptions or catch clauses handle exception, flow goes to next statement after all the catch clauses 6. call stack trace a. where the exception occurred b. can manually print reason for exception with getMessage() c. can manually print stack trace with printStackTrace() 7. EXAMPLE: see except2.java D) Catch and handle exception somewhere else 1. Method propogation a. exception is thrown b. no catch catches it c. control returned to method that made the initial call d. Java hopes that the calling method can catch the exception e. if the calling method won't catch, then the method that called the first method tries to catch. f. Java is very optimistic g. the process continues up until main method 2. Appropriate level? a. Programmer considers which level to catch exception b. EXAMPLE: see except3.java E) Who checks for exceptions? 1. some by compiler a. you must deal with exception b. or you can throw the exception c. ex) IOException e. throws { } f. means method will continue to propogate a checked exception d. ex) often seen in main(String args[]) throws IOException 2. Why should compiler check for "checked exceptions"? a. many methods could fail (like builtin methods from API) b. Java wants your program to succeed w/o break in flow c. so, checked exceptions by the comiler forces the programmer to account for things that may go wrong d. use try/catch and throws 3. some unchecked a. your program not need to deal with them 4. see except4.java F) Other features 1. throw a. causes an exception to propogate b. can customize exceptions 2. finally { } a. can be last clause in try statement b. will execture no matter how try is exited IV) Threads A) Normal flow (no threads) 1. Sequential 2. program goes top-to-bottom 3. statements exexcuted in given order B) Having threads 1. concurrent flow 2. different tasks exceute at the same time 3. think "parallel processing" C) Multithreaded 1. formal definition of program with threads 2. each thread is a sequential flow of execution 3. not necessarily executing the same statements D) Creating threads 1. two ways a. extend Thread class and override its run method b. create a class that implements Runnable interface and pass that object to to the constructor of newly created Thread object 2. EXAMPLE of (1a): thread1.java a. run method: tells Java what is concurrent b. start method: tells Java to start running a new thread 3. Beware of threads that share data! E) Synchronization 1. modify definition of method with synchonized 2. only one thread can call a method at a time 3. prevents sharing of data at same time by locking the object F) Controlling threads 1. suspend(): temporarily halt a thread's execution 2. resume(): resumes a suspended thread's exceution 3. sleep(long milliseconds): halt a threads exceution for a specific amount of time a. at least the given amount of time, maybe more b. good for pausing a program c. not tied to speed of processor