Java: exceptions and genericity

Similar documents
PARAMETRIC POLYMORPHISM

Announcements. Java Graphics. Exceptions. Java Odds & Ends

JAVA V Assertions Java, winter semester

JAVA V Source files Java, winter semester

Java: introduction to object-oriented features

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

CMSC 132: Object-Oriented Programming II

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

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

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

6.Introducing Classes 9. Exceptions

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

G51PGP Programming Paradigms. Lecture OO-4 Aggregation

Exercise 8 Parametric polymorphism November 18, 2016

Programming Languages and Techniques (CIS120)

COMP1008 Exceptions. Runtime Error

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

1.1. Annotations History Lesson - C/C++

Exceptions and Libraries

Programming II (CS300)

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

Exercise Session Week 6

Core Java Contents. Duration: 25 Hours (1 Month)

Java 5 New Language Features

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Generalized Code. Fall 2011 (Honors) 2

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

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

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

Java Fundamentals (II)

6.005 Elements of Software Construction

Exception-Handling Overview

Programming Languages and Techniques (CIS120e)

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

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Review Of Fundamental Concepts (Drinking from a Fire hose 101) Object Oriented Programming and Java

JAVA BASICS II. Example: FIFO

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

Java Generics Java, summer semester

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Java: advanced object-oriented features

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

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

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

PIC 20A Collections and Data Structures

The collections interfaces

Programming II (CS300)

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

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

Java Object Oriented Design. CSC207 Fall 2014

Self-review Questions

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS 3 Introduction to Software Engineering. 3: Exceptions

Java Generics -- an introduction. Based on

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CS Programming Language Java. Fall 2004 Sept. 29

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

15CS45 : OBJECT ORIENTED CONCEPTS

JDK 7 (2011.7) knight76.tistory.com Knight76 at gmail.com

CS 231 Data Structures and Algorithms Fall 2018

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Generics and Type Safety. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Introduction to Programming Using Java (98-388)

CSCI 261 Computer Science II

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

Sri Vidya College of Engineering & Technology Question Bank

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

Principles of Software Construction: Objects, Design and Concurrency. Exceptions, scope, static data and methods, and Generics.

Exceptions and assertions

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CS108, Stanford Handout #8. Java Generics

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

Chapter 14. Exception Handling and Event Handling

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

Subtyping for behaviour?

Errors and Exceptions

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

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

ECE 122. Engineering Problem Solving with Java

Comp215: Thinking Generically

More on Exception Handling

Full file at Chapter 2 - Inheritance and Exception Handling

Generics method and class definitions which involve type parameters.

Fundamental language mechanisms

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

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

Java Collections Framework

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Object typing and subtypes

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

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

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

Whidbey Enhancements to C# Jeff Vaughan MSBuild Team July 21, 2004

Transcription:

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: exceptions and genericity

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Exceptions

Exceptions Exceptions are objects Raise with a throw ExceptionObject instruction throw new AnExceptionClass( ErrorInfo ); Checked exceptions Declared in method signature: public void foo() throws SomeCheckedException Must be handled explicitly provide an exception handler (with a try/catch/finally block) propagate the exception (whose type is declared within the throws clause) to the caller Unchecked exceptions May be handled, if desired Unhandled exceptions terminate the current execution thread 3

Exception class hierarchy Throwable Exception Error Runtime Exception unchecked... checked unchecked 4

Exception handlers The scope of an exception handler is denoted by a try block Every try block is immediately followed by zero or more catch blocks, zero or one finally block, or both. At least one of catch blocks and finally block is required (otherwise, the try would be useless) public int foo(int b) { } try { if ( b > 3 ) { } throw new Exception(); } catch (Exception e) { b++; } finally { b++; } return b; 5

Exception handlers: catch blocks catch blocks can be exception-specific: catch (ExceptionType name) { /* handler */ } Targets exceptions whose type conforms to ExceptionType ExceptionType must be a descendant of Throwable name behaves as a local variable inside the handler block A catch block of type T cannot follow a catch block of type S if T S (otherwise the T-type block would be shadowed) Multi catch blocks (introduced in Java 7): catch (ET1 ET2 ET3 name) { /* handler */ } Targets exceptions whose type conforms to ET1, ET2, or ET3 ET1, ET2, and ET3 cannot be related by subclassing name behaves as a constant (final) inside the handler block 6

Exception handlers: catch/finally blocks When an exception of type T is thrown within a try block: control is transferred to the first (in textual order) catch block whose type T conforms to, if one exists then, the control is then transferred to the finally block (if it exists) finally, execution continues after the try block When no conforming catch exists or an exception is re-thrown inside the handler: After executing the finally block, the exception propagates to the next available enclosing handler When a try block terminates without exceptions: the control is transferred to the finally block (if it exists) then, execution continues after the try block 7

Exception handlers: catch/finally blocks A finally block is always executed after the try block even if no exceptions are thrown Typically used to free resources // foo() returns 2 (!) public int foo() { try { return 1; } finally { return 2; } } A control-flow breaking instruction (return, break, continue ) inside a finally block terminates the propagation of exceptions. // foo() returns 2 and propagates no exception public int foo() { } try {throw new Exception();} finally {return 2;} 8

Exception handlers A catch block may contain other try blocks From within a catch block an exception can be re-thrown: catch (Exception e) { if (...) {throw e;}...} Exceptions that propagate to the main method without being handled force termination of the program (typically, showing a trace of the call stack). 9

Catch, handle, and re-throw: example A method int readnum(string fn, int n) tries to read an n-digit integer from file with name fn. Exceptions handle things that may go wrong: a file with name s doesn t exist the file cannot be opened the file doesn t encode an integer the integer has fewer than n digits 10

Catch, handle, and re-throw: example public int readnum(string fn, int n) throws TooFewDigitsException, FileNotFoundException, IOException { int res; BufferedReader br = null; try { } br = new BufferedReader(new FileReader(fn)); String str = br.readline(); if (str.length < n) throw new TooFewDigitsException(str.length); res = Integer.parseInt(str); catch (FileNotFoundException e) { throw e; } catch (IOException e) { throw e; } catch (NumberFormatException e) { res = 0; } finally { if (br!= null) br.close(); } return res; } 11

Catch, handle, and re-throw: example Here s how a client may use readnum: int readint; String afilename; try { readint = n.readnum(afilename, 5); } catch (TooFewDigitsException e) { try { readint = n.readnum(filename, e.numread); } catch (Exception e) {System.out.println( Give up! );} } catch (Exception e) { System.out.println( IO error"); } 12

Try with resources Starting with Java 7, a try may also list some resources that are automatically closed after the block terminates (as normally done explicitly within a finally block). try ( FileOutputStream out = new FileOutputStream( o.txt"); FileInputStream in = new FileInputStream( i.txt"); ) {' // code that uses out and in } catch (IOException e) { /* Couldn t open files */ } catch and finally are completely optional in try-with-resources blocks (but checked exceptions must still be caught or propagated). A class must implement interface java.lang.autocloseable to be usable in a try-with-resources block. Basically, it needs a close() method 13

Checked vs. unchecked exceptions Checked exceptions are quite unique to Java C++ and C#, in particular, have only the equivalent of unchecked exceptions Which type of exception should you use in your Java programs? Java orthodoxy: checked exceptions should be the norm Rationale for preferring checked exceptions: exceptions usually carry information the client of a class should be informed about a method throwing unchecked exceptions is similar to a method with undocumented behavior clients are generally unprepared to deal with unexpected exceptions 14

Checked vs. unchecked exceptions Disadvantages of using checked exceptions extensively: lots of exception handling code to write lazy programmer s shortcut: empty catch blocks many catch blocks pollute code and decrease readability complex unwinding of the call stack to decide which exceptions to propagate and which to handle new exceptions change the interface of methods 15

Checked vs. unchecked exceptions How to strike a balance: As a norm, checked exceptions should replace error codes when the client should check the return code Use a checked exception if the caller can do something sensible with the exception useless with fatal errors whose causes are outside of the client s influence Document the usage of unchecked exceptions Don t use exceptions (checked or unchecked) when you should use assertions (contracts) see examples in C# slides of this class 16

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Genericity in Java

Generics Java s genericity mechanism, available since Java 5.0 Most common use: Use (and implement) generic type-safe containers ArrayList<String> safebox = new ArrayList<String>(); Compile-time type-checking is enforced More sophisticated uses: Custom generic classes and methods Bounded genericity (also called constrained genericity) public <T extends Interface1 & Interface2> T test(t x) 18

Generic classes A generic class is a class parameterized w.r.t. one or more generic types. public class Cell<T> { private T val; public T getval() { return val; } public void setval(t v) { val = v; } } To instantiate a generic class we must provide an actual type for the generic parameters. Cell<String> c = new Cell<String>(); 19

Generic classes The generic parameters of a generic class may constrain the valid actual types. public class Cell<T extends S> {... } The following is valid only if X is a subtype of S: Cell<X> c = new Cell<X>(); The constrains may involve multiple types. public class C<T extends String & Iterable> The following is valid only if Y is a subtype of both String and Iterable: C<Y> c = new C<Y>(); 20

Genericity before generics Before generics were available, using class Object was the way to achieve generic implementations. public class OldCell { private Object val; public Object getval() { return val; } public void setval(object v) { val = v; }} Requires explicit castings, with major problems: verbose code no compile-time checks OldCell c = new OldCell(); c.setval( A string ); // upcasting String s = (String) c.getval(); // downcasting Car c = (Car) c.getval(); // runtime error 21

Diamond operators and raw types When creating an instance of a generic class, the compiler is often able to infer the generic type from the context. In such cases, we can use the diamond operator. Cell<String> c = new Cell<>(); is equivalent to: Cell<String> c = new Cell<String>(); Generic classes can be instantiated as raw types, without providing any generic parameter. Raw types correspond to the old type-unsafe generic classes: Cell c = new Cell(); c.setval(12); // warning of unsafe behavior Cell<String> c = new Cell(); // not equivalent to new Cell<>()! 22

Generics: features and limitations Generic classes are translated into ordinary classes by the compiler: Process called type erasure The generic type is replaced by Object Casts are added as needed, after checking that they are type-safe Limitations of type erasure: Can t instantiate generic parameter with primitive types but can use wrapper classes At runtime you cannot tell the difference between ArrayList<Integer> and ArrayList<String> Exception classes cannot be generic classes Can t create objects of a generic type but can assign the value null to a variable of generic type Arrays with elements of a generic type parameter cannot be created A static member cannot reference a generic type parameter 23

Generics and inheritance Let S be a subtype of T (i.e. S T) There is no inheritance relation between: SomeGenericClass<S> and SomeGenericClass<T> In particular: the former is not a subtype of the latter However, let AClass be a non-generic type: T<AClass> is a subtype of T T denotes the raw type derived from the generic class T S<AClass> is a subtype of T<AClass> 24

Why subtyping with generics is tricky Consider a method of class F: public static void foo(linkedlist<vehicle> x){ } // add a Truck to the end of list x x.add(new Truck()); If LinkedList<Car> were a subtype of LinkedList<Vehicle>, this would be valid code: LinkedList<Vehicle> cars = new LinkedList<Car>(); cars.add(new Car()); F.foo(cars); But now a LinkedList<Car> would contain a Truck, which is not a Car! 25

Wildcards Give some polymorphic features to generics Unbounded wildcards: Collection<?> Collection of unkwnown(s) It is a super-type of Collection<T>, for any class T A method can read elements from a wildcard collection argument Can assign elements of the collection to references of type Object Cannot add new elements to the collection (see previous example) But it can add new null entries because null is a subtype of every other type 26

Bounded wildcards Bounded wildcards with upper bound: Collection<? extends X> It is a super-type of Collection<T>, for any subclass T of X A method can read elements from the wildcard collection argument Can assign elements of the collection to references of type X Cannot add new elements to the collection But it can add new null entries because null is a subtype of every other type 27

Upper-bounded wildcards: example Consider the following hierarchy of classes: Circle Shape Rectangle What should be the signature of a method drawshapes that takes a list of Shape objects and draws all of them? drawshapes( List<Shape> shapes ) this doesn t work on a List<Circle>, which is not a subtype of List<Shape> drawshapes( List<? extends Shape> shapes) this works on List<Shape>, List<Circle>, and List<Rectangle>, but doesn t work on List<Object> (correctly, as drawing is not defined for something that may not be a Shape) 28

Bounded wildcards Bounded wildcards with lower bound: Collection<? super X> It is a super-type of Collection<T>, for any superclass T of X A method can add elements to the collection (i.e., through the wildcard collection argument) Cannot assign elements of the collection to references of type X But it can read elements and assign them to reference of type Object because Object is a supertype of every other type Lower bounds are often used for write-only resources such as log streams. 29

Lower-bounded wildcards Consider a class for a list, including a sort method: class MySortedList <T> implements List {... void sort(comparator <T> cmp) {... }... } MySortedList<String> sl = new MySortedList<>(); Comparator<String> mc =... ; Comparator<Object> oc =... ; Valid call: sl.sort(mc); Invalid call: sl.sort(oc); Comparator<Object> is incompatible with Comparator<String> Solution: use a lower-bounded wildcard in sort s signature void sort(comparator <? super T> cmp) 30

Generic methods They are useful where wildcards fall short: adding elements to a generic collection Example: defining a method that assigns the elements in an array to a generic collection static void a2c(object[] a, Collection<?> c) { for (Object o : a) { c.add(o); /* Error */ } } We will know whether the type of o s elements is compatible with the type of c s elements only at runtime 31

Generic methods Example: defining a method that assigns the elements in an array to a generic collection Generic methods come to the rescue (notice the position of the generic parameter): static <G> void a2c(g[] a, Collection<G> c) { for (G o : a) { c.add(o); /* OK */ } } This is how client use the generic method. String[] arr = { Hello, world,! }; ArrayList<Object> lst = new ArrayList<>(); a2c(arr, lst); The actual generic parameter is inferred from context. 32

Collections A classic example of separating interface from implementation Some useful library interfaces from java.util: Collection<E> boolean add(e el) returns whether the collection actually changed void clear() remove all elements in the collection Iterator<E> iterator() returns an iterator over the collection Iterator<E> E next() void remove() removes the last element returned by the iterator 33

Collections: some implementations ArrayList: indexed, dynamically growing LinkedList: ordered, efficient insertion and removal HashSet: unordered, rejects duplicates TreeSet: ordered, rejects duplicates HashMap: key/value associations TreeMap: key/value associations, sorted keys 34

Java collections framework 35

Java collections framework 36

Java collections framework 37

Java collections framework 38