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

Slides:



Advertisements
Similar presentations
Exception Handling and Event Handling
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #20 & 21 Lecture #20 & 21 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications.
Yoshi
CS102--Object Oriented Programming
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Exception Handling Exception handling (EH) allows a programmer to provide code in the program to handle run-time errors or exceptional situations – this.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Chapter 14 Exception Handling and Event Handling.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
1 CS2200 Software Development Lectures 28: Exception Handling A. O’Riordan, 2008 (Includes some slides by Lewis/Loftus 2005 and K. Brown )
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
ISBN Chapter 14 Exception Handling and Event Handling.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11 Exception Handling and Event Handling.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
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.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Chapter 14 Exception Handling and Event Handling.
Object Oriented Programming
ISBN Chapter 14 Exception Handling and Event Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
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.
ISBN Chapter 14 Exception Handling and Event Handling.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
ISBN Chapter 14 Exception Handling and Event Handling.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Java Exceptions a quick review….
Exception Handling Exception handling (EH) allows a programmer to provide code in the program to handle run-time errors or exceptional situations this.
Exception Handling and Event Handling
Exception Handling and Event Handling
Exception and Event Handling
Exception Handling and Event Handling
Chapter 12 Exception Handling
Exception Handling and Event Handling
Web Design & Development Lecture 7
Exception Handling and Event Handling
Exception Handling and Event Handling
Chapter 12 Exception Handling and Text IO Part 1
Exception and Event Handling
Exception Handling and Event Handling
Java Basics Exception Handling.
Exception Handling and Event Handling
Presentation transcript:

May 21, 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

May 21, 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, :00AM-12:00PM Dater & Time: June 4, :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

May 21, 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

May 21, 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

May 21, 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

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

May 21, 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

May 21, 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

May 21, 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

May 21, 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(); }}

May 21, 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(); }}

May 21, 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)

May 21, 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(); }}

May 21, 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)

May 21, 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

May 21, 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

May 21, 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