Programming Language Concepts: Lecture 7

Similar documents
Programming Language Concepts: Lecture 7

Programming Language Concepts: Lecture 8

Programming Language Concepts: Lecture 6

ECE 122. Engineering Problem Solving with Java

Exceptions. CSC207 Winter 2017

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Exception-Handling Overview

Contents. I Introductory Examples. Topic 06 -Exception Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Programming Language Concepts: Lecture 9

Chapter 13 Exception Handling

Programming Language Concepts: Lecture 10

COMP1008 Exceptions. Runtime Error

Fundamentals of Object Oriented Programming

Errors and Exceptions

Assertions and Exceptions Lecture 11 Fall 2005

Abstract Classes, Exceptions

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Programming Language Concepts: Lecture 2

Java Programming Lecture 7

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Programming II (CS300)

Chapter 6 Introduction to Defining Classes

Today. Book-keeping. Exceptions. Subscribe to sipb-iap-java-students. Collections. Play with problem set 1

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Exceptions. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

List ADT. Announcements. The List interface. Implementing the List ADT

Programming II (CS300)

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

2.6 Error, exception and event handling

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Java Loose Ends. 11 December 2017 OSU CSE 1

Internal Classes and Exceptions

School of Informatics, University of Edinburgh

Exceptions and Design

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

National University. Faculty of Computer Since and Technology Object Oriented Programming

Cloning Enums. Cloning and Enums BIU OOP

Full file at Chapter 2 - Inheritance and Exception Handling

Object Oriented Programming

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

Programming II (CS300)

Instruction to students:

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Comp 249 Programming Methodology Chapter 9 Exception Handling

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

Programming Language Concepts: Lecture 2

Chapter 14. Exception Handling and Event Handling

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

BBM 102 Introduction to Programming II Spring Exceptions

CS 61B Discussion 5: Inheritance II Fall 2014

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

Tutorial 8 Date: 15/04/2014

Object-Oriented Design. March 2005 Object Oriented Design 1

Program Correctness and Efficiency. Chapter 2

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Super-Classes and sub-classes

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

2IP15 Programming Methods

BBM 102 Introduction to Programming II Spring 2017

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Exceptions and Libraries

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

Programming overview

Code Reuse: Inheritance

Chapter 4 Defining Classes I

Polymorphism. return a.doublevalue() + b.doublevalue();

Casting. References. References

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Object-Oriented Programming

Class, Variable, Constructor, Object, Method Questions

CSC 1214: Object-Oriented Programming

Object Oriented Programming. Week 7 Part 1 Exceptions

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CMSC 132: Object-Oriented Programming II

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Chapter 12 Exception Handling

Introduction to Software Design

Transcription:

Programming Language Concepts: Lecture 7 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in PLC 2011, Lecture 7, 01 February 2011

Exception handling Exception unexpected event that disrupts normal execution Different levels of severity Divide by zero End of file on read Need to recover from exceptions Take corrective action if possible Abort only if no option left Identifying the cause of an exception Need to go beyond rudimentary coding in terms of integer return values, as in C Exceptions have types!

Exception handling in Java If an error occurs, an operation throws an exception Information about the exception is encapsulated as an object Program in which the offending operation occurred should catch the exception object and handle it Analyze the object to determine the cause of the error Take corrective action if possible

Exception handling in Java All exceptions are subclasses of Throwable Two subclasses, Error and Exception Error problems beyond program s control An Error indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. Exception normally caught RunTimeException run time errors reported by JVM: divide-by-zero, array out-of-bounds

Exception handling in Java try-catch structure to handle exceptions try{ // Code that might generate error catch (ExceptionType1 e){ // Corrective code for ExceptionType1 catch (ExceptionType2 e){ // Corrective code for ExceptionType2 Error in try block generates exception object Exception object is sequentially checked against each catch What happens if first catch is catch (Throwable e1){

Exception handling in Java If some catch condition matches, appropriate code is executed If no catch matches, abort and propagate exception object up one level, to calling class

Exception handling in Java May need to do some cleanup (deallocate resources etc) This code may be left undone when an exception occurs Add a block labelled finally try{ catch (ExceptionType1 e){ catch (ExceptionType2 e){ finally{ // Always executed, whether try terminates normally // or exceptionally. Use for cleanup statements.

Customized exceptions Don t want negative values in a LinearList Define a new class extending Exception class NegativeException extends Exception{ private int error_value; // Stores negative value that generated exception public NegativeException(String message, int i){ super(message); // Appeal to superclass error_value = i; // constructor to set message public int report_error_value(){ return error_value;

Customized exceptions Inside LinearList class LinearList{ public void add(int i){ if (i < 0){ throw new NegativeException("Negative input",i);

Customized exceptions A program using LinearList should be aware that LinearList.add() can result in such an exception Exception should be advertized by LinearList class LinearList{ public void add(int i) throws NegativeException{ Need not advertize exceptions of type Error or RunTimeException

Customized exceptions Using LinearList.add() with customized exception LinearList l = new LinearList(); try{ l.add(i); catch (NegativeException ne){ System.out.print("Negative input supplied was "); System.out.print(ne.report_error_value());

Parameter passing in Java Scalars are passed by value No way to write a function to swap two ints Objects are passed by reference How do we swap two objects? class Myclass{ public void swap(myclass p){ // Swap "this" with p Myclass tmp; tmp = p; p = this; this = tmp; Will not work!

Parameter passing Instead, we must write something like: class Myclass{ public void swap(myclass p){ Myclass tmp = new Myclass(); // Make a new tmp ob // Copy contents of p into tmp // Copy contents of this into p // Copy contents of tmp back into this

Parameter passing Return values? Suppose we add a function to Employee class Employee{ // "accessor" methods public Date get_joindate(){ return joindate; Now we write Employee e = new Employee(); Date d = e.get_joindate(); d.advance(100); // e loses 100 days seniority! Get public access to a private field of Employee Should make a copy of joindate before returning it

Cloning Object class defines Object clone(object o) Makes a bit-wise copy Nested objects will not be cloned automatically! To use clone, must implement Cloneable class Employee implements Cloneable{ Marker interface empty! Inside clone(), expect a check such as Object clone(object o){ if (o instanceof Cloneable){ // go ahead and clone else{ // complain and quit

Packages Java has an organizational unit called package By default, all classes in a directory belong to a package If neither public nor private is specified, visibility is with respect to package Can use import to use packages directly or import java.math.bigdecimal import java.math.* All classes in /java/math Note that * is not recursive

Protected protected means visible within subtree Normally, a subclass cannot change visibility of a function However, protected can be made public clone() is defined as protected

GUIs and event driven programming How do we design graphical user interfaces? Multiple applications simultaneously displayed on screen Keystrokes, mouse clicks have to be sent to appropriate window In parallel to main activity, record and respond to these events Web browser renders current page Clicking on a link loads a different page

Keeping track of events Low level solution Remember coordinates and extent of each window Track coordinates of mouse OS reports mouse click at (x,y) Check which windows are positioned at (x,y) Check if one of them is active Inform that window about mouse click Tedious and error-prone

Keeping track of events Better solution Programming language support for higher level events Button was clicked, box was ticked OS reports low level events Mouse clicked at (x,y), key a pressed Run time support for language maps low level events to high level events

Keeping track of events Better solution Programmer directly defines components such as windows, buttons, that generate high level events Each event is associated with a listener that knows what to do e.g., clicking Close window exits application Programming language has mechanisms for Describing what types of events a component can generate Setting up an association between components and listeners Different events invoke different functions Window frame has Maximize, Iconify, Close buttons Language sorts out events and automatically calls the correct function in the listener