* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download ppt - kaist
Survey
Document related concepts
Scala (programming language) wikipedia , lookup
Stream processing wikipedia , lookup
Go (programming language) wikipedia , lookup
Java (programming language) wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Error detection and correction wikipedia , lookup
C Sharp syntax wikipedia , lookup
Functional programming wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Object-relational impedance mismatch wikipedia , lookup
Java ConcurrentMap wikipedia , lookup
Programming language wikipedia , lookup
Java performance wikipedia , lookup
Reactive programming wikipedia , lookup
Object-oriented programming wikipedia , lookup
Transcript
Programming Languages (ICE 1341) Lecture #21 May 21, 2004 In-Young Ko iko .AT. icu.ac.kr Information and Communications University (ICU) May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 1 Announcements Project Presentations Dater & Time: Thursday May 27th, 7:30PM – 9:30PM 10 minutes for each team (in English) Final Exam: Dater & Time: June 4, 2004 11:00AM-12:00PM A hand-written cheat sheet is allowed Chapters that will be covered: May 21, 2004 Chapter 8 – 11 Chapter 12 except 12.7, 12.8 and 12.9 Chapter 13 except 13.7 Chapter 14 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 2 Review of the Previous Lectures Semaphores Synchronization Problems Dining Philosophers Problem Monitors Message Passing Java Threads Statement-Level Concurrency May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 3 Exception Handling 30 OPEN (UNIT=10, FILE='data.txt', STATUS='OLD') READ(10, *, END=40, ERR=50) NUM … GOTO 30 FORTRAN Exception: An unexpected condition that is raised (thrown) during program execution Hardware Detectable Exceptions: e.g., Floating-point overflow, I/O errors Software Detectable Exceptions: e.g., Array subscript range errors, null pointer exceptions Exception Handling: The special processing that may be required after detection of an exception is called Exception Handler: The exception handling code unit May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 4 Exception Handling Example void fileCopy(String file1, String file2) { try { FileInputStream in = new FileInputStream(file1); FileOutputStream out = new FileOutputStream(file2); int data; while ((data = in.read()) >= 0) out.write(data); } catch (FileNotFoundException e1) { System.err.println (“Cannot open input or output file.”); } catch (IOException e2) { System.err.println (“Cannot read or write data.”); } } Java May 21, 2004 May throw the File Not Found Exception May throw the IO Exception Exception Handlers ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 5 Exception Descriptions in the Java API Manual May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 6 Exception Handling Control Flow Main Program Control Main Program Control Catching the exception Exception Uncaught exception Exception Exception Handler Continuation of the main program May 21, 2004 Operating System Discontinuation of the main program ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 7 Advantages of Built-in Exception Handling Programmers do not need to explicitly define, detect, and raise exceptions Exception propagation allows a high level of reuse of exception handling code * AW Lecture Notes May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 8 Exceptions in Java All exceptions are objects of classes that are descendants of the Throwable class Serious problems that a user program should not try to catch e.g., Heap overflow Program errors e.g., Array index out-of-bound, Null pointer exception Exception … … IOException RunTimeException Unchecked Exceptions • User programs are not required to handle them • The compiler do not concern them May 21, 2004 Error … FileNotFoundException MyIOException (User-defined Exception) Checked Exceptions ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 9 Exception Handling in Java void fileCopy(String file1, String file2) { Exceptions cannot be try { disabled FileInputStream in = new FileInputStream(file1); Binding Exceptions to FileOutputStream out = Handlers – An exception new FileOutputStream(file2); is bound to the first int data; handler with a parameter while ((data = in.read()) >= 0) out.write(data); is the same class as the } catch (FileNotFoundException e1) { thrown object or an System.err.println(“Can’t open a file.”); ancestor of it } catch (IOException e2) { System.err.println(“Can’t read or write.”); Specify code that is to be } finally { executed, regardless of in.close(); what happens in the try out.close(); construct } } May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 10 Exception Propagation in Java void fileCopy(String file1, String file2) throws FileNotFoundException, IOException { FileInputStream in = new FileInputStream(file1); FileOutputStream out = new FileOutputStream(file2); int data; while ((data = in.read()) >= 0) out.write(data); in.close(); out.close(); } void myMain() { try { fileCopy (“source.txt”, “dest.txt”); } catch (Exception e) { e.printStackTrace(); } } May 21, 2004 A method can be declared to propagate certain exceptions to its caller by using ‘throws’ Exceptions are dynamically bound to handlers To insure that all exceptions are caught, a handler can be defined to have an Exception class parameter There are built-in operations in exception objects ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 11 Exception Stack Trace in Java Java’s exception stack trace shows the dynamic chain of exception propagations java.io.FileNotFoundException: source.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at ExceptionTest.fileCopy(ExceptionTest.java:9) at ExceptionTest.myMain(ExceptionTest.java:20) at ExceptionTest.main(ExceptionTest.java:30) May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 12 User-defined Exceptions in Java void fileCopy(String file1, String file2) throws MyIOException { try { FileInputStream in = new FileInputStream(file1); FileOutputStream out = new FileOutputStream(file2); int data; while ((data = in.read()) >= 0) out.write(data); in.close(); out.close(); } catch (Exception e) { throw new MyIOException(); } } May 21, 2004 User-defined exceptions can be thrown by throw Rethrowing (reraising) an exception ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 13 Exception Handling in Ada (1) Exception handlers are bound for program blocks: begine -- block body exception when exception_name { | exception_name } => statement_sequence when ... [when others => statement_sequence] end; User-defined Exceptions: exception_name_list : exception; raise [exception_name] Exception conditions can be disabled with (‘pragma’ is a directive to the compiler): pragma SUPPRESS(exception_list) May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 14 Exception Handling in Ada (2) Predefined Exceptions: CONSTRAINT_ERROR - index constraints, range constraints, etc. NUMERIC_ERROR - numeric operation cannot return a correct value (overflow, division by zero, etc.) PROGRAM_ERROR - call to a subprogram whose body has not been elaborated STORAGE_ERROR - system runs out of heap TASKING_ERROR - an error associated with tasks * AW Lecture Notes May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 15 Exception Handling in C++ Exceptions are bound to handlers through the type of the parameter: int new_grade; try { if (index < 0 || index > 9) throw (new_grade); … } catch (int grade) { if (grade == 100) … } All exceptions are user-defined A throw without an operand can only appear in a handler; when it appears, it simply reraises the exception, which is then handled elsewhere May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 16 Event Handling in Java An event is created by an external action such as a user interaction through a GUI The event handler is a segment of code that is called in response to an event A JButton Button Pressed Event Event Listeners JButton helloButton = new JButton(“Hello”); helloButton.addActionListener(new AbstractAction() { public void actionPerformed(ActionEvent e) { System.out.println(“Hello World!”); } } May 21, 2004 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko 17