Presentation is loading. Please wait.

Presentation is loading. Please wait.

May 21, 2004 1 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Programming Languages (ICE 1341) Lecture #21 Programming Languages (ICE 1341)

Similar presentations


Presentation on theme: "May 21, 2004 1 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Programming Languages (ICE 1341) Lecture #21 Programming Languages (ICE 1341)"— Presentation transcript:

1 May 21, 2004 1 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Programming Languages (ICE 1341) Lecture #21 Programming Languages (ICE 1341) Lecture #21 May 21, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr

2 May 21, 2004 2 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Announcements Project Presentations Project Presentations Dater & Time: Thursday May 27 th, 7:30PM – 9:30PM Dater & Time: Thursday May 27 th, 7:30PM – 9:30PM 10 minutes for each team (in English) 10 minutes for each team (in English) Final Exam: Final Exam: Dater & Time: June 4, 2004 11:00AM-12:00PM Dater & Time: June 4, 2004 11:00AM-12:00PM A hand-written cheat sheet is allowed A hand-written cheat sheet is allowed Chapters that will be covered: Chapters that will be covered: Chapter 8 – 11 Chapter 8 – 11 Chapter 12 except 12.7, 12.8 and 12.9 Chapter 12 except 12.7, 12.8 and 12.9 Chapter 13 except 13.7 Chapter 13 except 13.7 Chapter 14 Chapter 14

3 May 21, 2004 3 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Review of the Previous Lectures Semaphores Semaphores Synchronization Problems Synchronization Problems Dining Philosophers Problem Dining Philosophers Problem Monitors Monitors Message Passing Message Passing Java Threads Java Threads Statement-Level Concurrency Statement-Level Concurrency

4 May 21, 2004 4 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Handling Exception: An unexpected condition that is raised (thrown) during program execution Exception: An unexpected condition that is raised (thrown) during program execution Hardware Detectable Exceptions: e.g., Floating-point overflow, I/O errors Hardware Detectable Exceptions: e.g., Floating-point overflow, I/O errors Software Detectable Exceptions: e.g., Array subscript range errors, null pointer exceptions 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 Handling: The special processing that may be required after detection of an exception is called Exception Handler: The exception handling code unit Exception Handler: The exception handling code unit OPEN (UNIT=10, FILE='data.txt', STATUS='OLD') OPEN (UNIT=10, FILE='data.txt', STATUS='OLD') 30 READ(10, *, END=40, ERR=50) NUM … GOTO 30 GOTO 30 FORTRAN

5 May 21, 2004 5 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Handling Example void fileCopy(String file1, String file2) { try { try { FileInputStream in = FileInputStream in = new FileInputStream(file1); new FileInputStream(file1); FileOutputStream out = FileOutputStream out = new FileOutputStream(file2); new FileOutputStream(file2); int data; int data; while ((data = in.read()) >= 0) while ((data = in.read()) >= 0) out.write(data); out.write(data); } catch (FileNotFoundException e1) { } catch (FileNotFoundException e1) { System.err.println System.err.println (“Cannot open input or output file.”); (“Cannot open input or output file.”); } catch (IOException e2) { } catch (IOException e2) { System.err.println System.err.println (“Cannot read or write data.”); (“Cannot read or write data.”); }} May throw the File Not Found Exception May throw the IO Exception Exception Handlers Java

6 May 21, 2004 6 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Descriptions in the Java API Manual

7 May 21, 2004 7 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Handling Control Flow Main Program Control Exception Exception Handler Continuation of the main program Catching the exception Main Program Control Exception Operating System Discontinuation of the main program Uncaught exception

8 May 21, 2004 8 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Advantages of Built-in Exception Handling Programmers do not need to explicitly define, detect, and raise exceptions Programmers do not need to explicitly define, detect, and raise exceptions Exception propagation allows a high level of reuse of exception handling code Exception propagation allows a high level of reuse of exception handling code * AW Lecture Notes

9 May 21, 2004 9 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exceptions in Java All exceptions are objects of classes that are descendants of the Throwable class 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 MyIOException (User-defined Exception) Error … Exception IOException … FileNotFoundException… RunTimeException Unchecked Exceptions User programs are not required to handle themUser programs are not required to handle them The compiler do not concern themThe compiler do not concern them Checked Exceptions Program errors e.g., Array index out-of-bound, Null pointer exception

10 May 21, 2004 10 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Handling in Java Exceptions cannot be disabled Exceptions cannot be disabled Binding Exceptions to Handlers – An exception is bound to the first handler with a parameter is the same class as the thrown object or an ancestor of it Binding Exceptions to Handlers – An exception is bound to the first handler with a parameter is the same class as the thrown object or an ancestor of it Specify code that is to be executed, regardless of what happens in the try construct Specify code that is to be executed, regardless of what happens in the try construct void fileCopy(String file1, String file2) { try { try { FileInputStream in = FileInputStream in = new FileInputStream(file1); new FileInputStream(file1); FileOutputStream out = FileOutputStream out = new FileOutputStream(file2); new FileOutputStream(file2); int data; int data; while ((data = in.read()) >= 0) while ((data = in.read()) >= 0) out.write(data); out.write(data); } catch (FileNotFoundException e1) { } catch (FileNotFoundException e1) { System.err.println(“Can’t open a file.”); System.err.println(“Can’t open a file.”); } catch (IOException e2) { } catch (IOException e2) { System.err.println(“Can’t read or write.”); System.err.println(“Can’t read or write.”); } finally { } finally { in.close(); in.close(); out.close(); out.close(); }}

11 May 21, 2004 11 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Propagation in Java A method can be declared to propagate certain exceptions to its caller by using ‘throws’ A method can be declared to propagate certain exceptions to its caller by using ‘throws’ Exceptions are dynamically bound to handlers Exceptions are dynamically bound to handlers To insure that all exceptions are caught, a handler can be defined to have an Exception class parameter 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 There are built-in operations in exception objects void fileCopy(String file1, String file2) throws FileNotFoundException, throws FileNotFoundException, IOException { IOException { FileInputStream in = FileInputStream in = new FileInputStream(file1); new FileInputStream(file1); FileOutputStream out = FileOutputStream out = new FileOutputStream(file2); new FileOutputStream(file2); int data; int data; while ((data = in.read()) >= 0) while ((data = in.read()) >= 0) out.write(data); out.write(data); in.close(); out.close(); in.close(); out.close();} void myMain() { try { try { fileCopy (“source.txt”, “dest.txt”); fileCopy (“source.txt”, “dest.txt”); } catch (Exception e) { } catch (Exception e) { e.printStackTrace(); e.printStackTrace(); }}

12 May 21, 2004 12 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Stack Trace in Java Java’s exception stack trace shows the dynamic chain of exception propagations 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. (Unknown Source) at ExceptionTest.fileCopy(ExceptionTest.java:9) at ExceptionTest.myMain(ExceptionTest.java:20) at ExceptionTest.main(ExceptionTest.java:30)

13 May 21, 2004 13 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko User-defined Exceptions in Java User-defined exceptions can be thrown by throw User-defined exceptions can be thrown by throw Rethrowing (reraising) an exception Rethrowing (reraising) an exception void fileCopy(String file1, String file2) throws MyIOException { throws MyIOException { try { try { FileInputStream in = FileInputStream in = new FileInputStream(file1); new FileInputStream(file1); FileOutputStream out = FileOutputStream out = new FileOutputStream(file2); new FileOutputStream(file2); int data; int data; while ((data = in.read()) >= 0) while ((data = in.read()) >= 0) out.write(data); out.write(data); in.close(); out.close(); in.close(); out.close(); } catch (Exception e) { } catch (Exception e) { throw new MyIOException(); throw new MyIOException(); }}

14 May 21, 2004 14 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Handling in Ada (1) Exception handlers are bound for program blocks: Exception handlers are bound for program blocks:begine -- block body -- block body exception exception when exception_name { | exception_name } => statement_sequence when exception_name { | exception_name } => statement_sequence when... when... [when others => statement_sequence] [when others => statement_sequence]end; User-defined Exceptions: User-defined Exceptions: exception_name_list : exception; raise [exception_name] Exception conditions can be disabled with (‘pragma’ is a directive to the compiler): Exception conditions can be disabled with (‘pragma’ is a directive to the compiler): pragma SUPPRESS(exception_list)

15 May 21, 2004 15 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Handling in Ada (2) Predefined Exceptions: Predefined Exceptions: CONSTRAINT_ERROR - index constraints, range constraints, etc. CONSTRAINT_ERROR - index constraints, range constraints, etc. NUMERIC_ERROR - numeric operation cannot return a correct value (overflow, division by zero, 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 PROGRAM_ERROR - call to a subprogram whose body has not been elaborated STORAGE_ERROR - system runs out of heap STORAGE_ERROR - system runs out of heap TASKING_ERROR - an error associated with tasks TASKING_ERROR - an error associated with tasks * AW Lecture Notes

16 May 21, 2004 16 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Exception Handling in C++ Exceptions are bound to handlers through the type of the parameter: Exceptions are bound to handlers through the type of the parameter: int new_grade; int new_grade; try { if (index 9) try { if (index 9) throw (new_grade); … throw (new_grade); … } catch (int grade) { } catch (int grade) { if (grade == 100) … if (grade == 100) … } All exceptions are user-defined 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 A throw without an operand can only appear in a handler; when it appears, it simply reraises the exception, which is then handled elsewhere

17 May 21, 2004 17 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Event Handling in Java An event is created by an external action such as a user interaction through a GUI 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 The event handler is a segment of code that is called in response to an event JButton helloButton = new JButton(“Hello”); JButton helloButton = new JButton(“Hello”); helloButton.addActionListener(new AbstractAction() { helloButton.addActionListener(new AbstractAction() { public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { System.out.println(“Hello World!”); System.out.println(“Hello World!”); } } A JButton Event Listeners Button Pressed Event


Download ppt "May 21, 2004 1 ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Programming Languages (ICE 1341) Lecture #21 Programming Languages (ICE 1341)"

Similar presentations


Ads by Google