Data Structures. Subodh Kumar. Dept of Computer Sc. & Engg. IIT Delhi

Similar documents
14. Exception Handling

Program Correctness and Efficiency. Chapter 2

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

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

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

Chapter 14. Exception Handling and Event Handling ISBN

Defensive Programming

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

Chapter 14. Exception Handling and Event Handling

Java Bytecode (binary file)

BBM 102 Introduction to Programming II Spring Exceptions

CS159. Nathan Sprague

CSE 331 Software Design & Implementation

Exceptions and assertions

Fundamentals of Object Oriented Programming

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

Sri Vidya College of Engineering & Technology Question Bank

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

Assertions, pre/postconditions

COMP1008 Exceptions. Runtime Error

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Errors and Exceptions

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

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

Chapter 14. Exception Handling and Event Handling 异常处理和事件处理. 孟小亮 Xiaoliang MENG, 答疑 ISBN

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Class, Variable, Constructor, Object, Method Questions

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Correctness and Robustness

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

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

16-Dec-10. Consider the following method:

Exceptions Handling Errors using Exceptions


Exceptions and Libraries

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSCI 261 Computer Science II

Assertions and Exceptions Lecture 11 Fall 2005

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

Object Oriented Software Design

Object Oriented Software Design

BBM 102 Introduction to Programming II Spring 2017

Exception-Handling Overview

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Types, Values and Variables (Chapter 4, JLS)

Comp 249 Programming Methodology Chapter 9 Exception Handling

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

Programming II (CS300)

CS 11 java track: lecture 3

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

Programming II (CS300)

Programming II (CS300)

EXCEPTION HANDLING. Summer 2018

Chapter 4 Defining Classes I

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

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

Exceptions. CSC207 Winter 2017

Static program checking and verification

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

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

CS193j, Stanford Handout #25. Exceptions

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Java Programming Unit 7. Error Handling. Excep8ons.

Std 12 Lesson-10 Exception Handling in Java ( 1

Chapter 11, Testing. Using UML, Patterns, and Java. Object-Oriented Software Engineering

Exceptions. Examples of code which shows the syntax and all that

Full file at Chapter 2 - Inheritance and Exception Handling

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

Exception Handling. Content 25/03/15. Advanced Programming

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

More on Exception Handling

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

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

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

CS 3 Introduction to Software Engineering. 3: Exceptions

Introduction to Java. Handout-3a. cs402 - Spring

An exception is simply an error. Instead of saying an error occurred, we say that an.

13: Error Handling with Exceptions. The basic philosophy of Java is that "badly formed code will not be run."

Software Testing. Overview

Download link: Java Exception Handling

Object-Oriented Design. March 2005 Object Oriented Design 1

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

COP4020 Programming Languages. Exception Handling Prof. Robert van Engelen

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

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

ASSERTIONS AND LOGGING

There are three basic elements in object oriented programming: encapsulation, inheritance and polymorphism.

Programming II (CS300)

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

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

Chapter 13 Exception Handling

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Transcription:

Data Structures Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhi

OOP Quiz 2 Class Bird extends class Animal. class Cat { public Bird catchbird(bird birdtocatch) {... Later in a method Animal pigeon = new Bird(); catchbird(pigeon); Type mismatch: compiler Is there a problem? 24

OOP Quiz 3 In class Animal: public void eat(food food) { food.cook(); food = new Food(); food.digest(); Somewhere else in the program: myanimal.eat(apple);// apple is of type Fruit // Fruit is a subclass of Food What happens to object apple? 1) Nothing; only a copy of apple gets passed as a parameter, original is safe 2) It gets cooked, but not digested 3) It gets cooked and digested Cooked. Some other Food object is digested. 4) It gets digested, but not cooked 5) After eat, apple refers to the same instance of the Fruit class as it did before we called eat() apple in caller still refers to the same object 6) After eat returns, apple refers to a different instance of the Fruit class 25

OOP Quiz 4 F T T F True/False? 1. If class B extends A, which has a method named m: any method by name m declared in B must have the same signature as the one in A. 2. Formal parameters have the same scope as local variables (of the method). 3. An actual parameter may be an instance of a subclass of the type declared in the formal parameter. 4. An instance of a subclass may be substituted for an instance of its superclass in an actual parameter just as an instance of a superclass may serve as the substitute for an instance of its subclass in the same situation. 26

class C1 { C1() { System.out.println("C1"); C1(int n) { System.out.println("C1 : " + n); void print() { System.out.println("C1 print"); class C2 extends C1 { C2() { System.out.println("C2"); C2(int n) { this(); System.out.println("C2 : " + n); void print() { System.out.println("C2 print"); class MainClass { public static void main(string[] args) { C1[] c1 = new C1[4]; c1[0] = new C2(); DYI. Run it to verify. c1[1] = new C2(5); c1[2] = new C1(); c1[3] = new C1(4); for (int i=0; i<4; i++) { c1[i].print(); What prints? 27

Lucid Programming Style! One class per file! Indent: 2-3 spaces! Use names describing functionality! Do not reuse variables! Capitalize parts Good choice Professor teachsections4to8 col106professor numsections! Begin class names with capital letters! Begin method, local and instance variable names with small letters! Constants in all caps, use _ to separate parts! Begin package names with small letters Poor choice myclass operate x number, count 28

Example Conventions! Write short methods! Declare at the beginning! of method, block, loop! at least be consistent! No literals (except for -1, 0, and 1 in loops)! Qualify names fully! Access static methods by class name! Use parentheses liberally! Declare public only when necessary! Declare all fields then methods in a class! Constructor first, Overloads together! Initialize basic type fields at declaration 29

Testing! Test, then test some more! Test in unit environment! Test full application! Maintain regression suite! Directed tests for hard/corner cases! Ensure code coverage! Random test generation! Use svn, regress check-ins! Assertion checks (See assert expr1:expr2)! Validate input 30

Be Mindful of Common Errors! Array index out of bounds! File not found! Disk full! Permission denied! Type conversion! Null reference! Network down! Divide by 0 31

Exceptions are your friend! Helps you handle run-time errors! And think about types of errors that are possible! Method raises exception to indicate unexpected condition! Implicit return flags! Catch Expected exception and handle it! Separates error handling in a modular way! Eases passing the buck to caller! Unhandled exception terminates the program! Program should handle user defined exceptions 32

Unchecked Exception public class Propagate { void divide() { int m = 25, i = 0; i = m / i; Excep'on void process() { divide(); public static void main(string[] args) { Propagate p = new Propagate(); p.process(); java.lang.arithmeticexception: / by zero at Propagate.orange(Propagate.java:4) at Propagate.apple(Propagate.java:8) at Propagate.main(Propagate.java:11) JAVA default handler 33

Exception Handling try { normal program code catch(exception e) { exception handling code finally { // optional: execute after try 34

Exception Handling try { normal program code catch(someexceptionclass e) { exception handling code catch(someexceptionclass e) { exception handling code finally { // this is optional 35

System Exception public void amethod() { try { int a[] = new int[2]; a[2] = 1; catch (ArrayIndexOutOfBoundsException e) { System.out.println( "exception: " + e.getmessage()); e.printstacktrace(); 36

Pass Exception Along! A method that might encounter an unhandled exception, should use throws clause: public void mymethod throws IOException { normal code with some I/O! It can generate exception as well 37

Implementation! Exception are objects! Instances of Throwable class, or one derived from it! You should makes new class! extending JAVA Exception class! class Exception extends Throwable class MyException extends Exception { 38

Example class MyException extends Exception { class MyClass { void somemethod() throws Myexception { MyException x = new MyException(); //... some code here if (val < 1) throw x; 39

Example class MyException extends Exception { MyException(String s) { super(s); void somemethod throws MyException { MyException x = new MyException( Message );... if (val < 1) throw x; 40

Program Correctness! Starts with a correct specification of the requirements! Correctness only with respect to the spec! If program A sends data D to program B, B will eventually get D! There is no deadlock or livelock! For all n 0, f(n) = k! Correct design! Algorithmically correct! Correct implementation! Re-use of already correct code helps 41

Programming! Understand specification! Formulate as formally as you can! Devise the test plan! Algorithmic design! Mind the performance Repeat as necessary! Refine the test plan! Implement incrementally! Test each time! Error debugging + performance debugging 42

Proving Correctness! What is correct?! Program? Or entire running enviroment?! System! What is a proof?! Not: system meets requirement! But: system cannot fail requirement! Challenging 43

Testing is Important Bill Gates: When you look at a big commercial software company like Microsoft, there's actually as much testing that goes in as development. We have as many testers as we have developers. Testers basically test all the time, and developers basically are involved in the testing process about half the time We've probably changed the industry we're in. We're not in the software industry; we're in the testing industry, and writing the software is the thing that keeps us busy doing all that testing The test cases are unbelievably expensive; in fact, there's more lines of code in the test harness than there is in the program itself. Often that's a ratio of about three to one. 44

Proof? Dijkstra: Program testing can be used to show the presence of bugs, but never to show their absence One can never guarantee that a proof is correct, the best one can say is: I have not discovered any mistakes! Automated proof?! Impossible in the general case! Mechanical verification is possible in some cases 45

Formal Specification! Precise syntax and semantics! Another language! Maybe, programming in this language is simpler! It only needs to specify behavior! Specification of the system is not:! specification of some of its properties! Specification could be given as! actual program! state machine! set of logical expressions 46

Example Verification Methods! Invariants! Precondition Postcondition! Condition X is impossible! Condition Y is true in every execution! Condition Z is always eventually true! Value of a variable is f(..) at time > t0! Given state machine, show <P> satisfied 01 00 11 10 <P>: NEVER IN STATE 10 47