Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 7. Nadia Polikarpova

Similar documents
Exercise Session Week 6

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 8.

CSPP : Introduction to Object-Oriented Programming

Exercise Session Week 8

CSCE 314 Programming Languages

Exercise Session Week 8

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: reflection

Exceptions Handling Errors using Exceptions

6.005 Elements of Software Construction

COMP1008 Exceptions. Runtime Error

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 3

JAVA V Assertions Java, winter semester

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?

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

Software Engineering. Prof. Agostino Poggi

Java and C# in Depth

Writing your own Exceptions. How to extend Exception

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

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 9. Nadia Polikarpova

Exception handling & logging Best Practices. Angelin

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

COMP-202 Unit 9: Exceptions

Java: exceptions and genericity

#Students per correct answers

JAVA V Source files Java, winter semester

Exception Handling Generics. Amit Gupta

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

Java Exceptions Java, winter semester

AP CS Unit 7: Interfaces. Programs

Fundamentals of Object Oriented Programming

CS108, Stanford Handout #8. Java Generics

Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski

Rules and syntax for inheritance. The boring stuff

Exceptions. What exceptional things might our programs run in to?

Programming - 2. Common Errors

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

Programming II (CS300)

Exceptions and Libraries

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

COMP-202 Unit 9: Exceptions

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 10

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

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

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

Programming II (CS300)

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Inheritance (Part 2) Notes Chapter 6

Introduction Unit 4: Input, output and exceptions

Java Exceptions Version June 2009

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

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

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

Exception-Handling Overview

Full file at Chapter 2 - Inheritance and Exception Handling

Exceptions - Example. Exceptions - Example

Checked and Unchecked Exceptions in Java

Exercise Session Week 7

GlobalLogic Technical Question Paper

Java Fundamentals (II)

Fall 2017 CISC124 10/1/2017

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

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

Parametric polymorphism and Generics

CS 2230 CS II: Data structures. Meeting 20: generic types, exceptions, higher order functions Brandon Myers University of Iowa

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

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

Generics method and class definitions which involve type parameters.

Object Oriented Programming. Week 7 Part 1 Exceptions

ASSERTIONS AND LOGGING

INSTRUCTIONS TO CANDIDATES

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

Timing ListOperations

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. C#: reflection

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Here is a hierarchy of classes to deal with Input and Output streams.

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Object Oriented Software Design

Object Oriented Software Design

Lecture 19 Programming Exceptions CSE11 Fall 2013

Java Generics Java, summer semester

Java Generics -- an introduction. Based on

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

Casting. References. References

15CS45 : OBJECT ORIENTED CONCEPTS

CS 231 Data Structures and Algorithms Fall 2018

For more details on SUN Certifications, visit

2018/2/5 话费券企业客户接入文档 语雀

The collections interfaces

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

Binary search. int index = Collections.binarySearch(list, element);

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

Transcription:

Chair of Software Engineering Java and C# in Depth Prof. Dr. Bertrand Meyer Exercise Session 7 Nadia Polikarpova

Quiz 1: Does it compile? (Java) public class MyException extends Exception { Checked exception... public MyException(String message) { super(message); public class A { public void dosomething() throws MyException { if (badstuffhappened) { dosomething throws a checked throw new MyException( Help! ) exception, but does not declare it } else {... } 2

Quiz 1 (cont.): Does it compile? (Java) public class A { public void dosomething() throws MyException {... public class B { public void domore() { A a;... a.dosomething(); domore calls a method, which throws a checked exception, but doesn t itself declare or catch it 3

Quiz 2: Exception accumulation (Java) private String readdatafromurl(string url) throws MalformedURLException { // throw MalformedURLException if url is bad. // read data and return it. } private long parselong(string data) throws NumberFormatException { // convert data to long. // throw NumberFormatException if data is not a valid long. } public long readnumberfromurl(string url) throws MalformedURLException, NumberFormatException { String data = readdatafromurl(url); long number = parselong(data); return number; } How to avoid the accumulation of exception declarations up in the call hierarchy? 4

Quiz 2: solutions Solution 1: Wrapping public void readnumberfromurl(string url) throws ApplicationException{ try { String data = readdatafromurl(url); long number = parselong(data); } catch (MalformedUrlException e) { throw new ApplicationException(e); } catch (NumberFormatException e) { throw new ApplicationException(e); } } Solution 2: Inheritance public long readnumberfromurl(string url) throws Exception {... } 5

Quiz 3: What will happen? (Java) Suppose myfile.txt does not exist. InputStream input = null; Which exception will the client get? try { input = new FileInputStream("myFile.txt"); //do something with the stream } catch(ioexception e) { throw new WrappedException(e); } finally { try { input is null input.close(); } catch(ioexception e) { throw new WrappedException(e); } NullPointerException is thrown inside the } finally block and propagated to the client! 6

Quiz 4: Does it compile? (Java) List<Integer> li = new ArrayList<Integer>(); List<Number> ln = li; Generics are not covariant! List<String>[] lsa = new List<String>[10]; List<?>[] lsa = new List<?>[3] Cannot create arrays of generic types This is fine public class DecimalString implements Comparable<Number>, Comparable<String> {... Comparable<Number> and Comparable<String> are the same interface! 7

Quiz 4: Does it compile? (C#) List<int> li = new List<int>(); List<Object> lo = li; Generics are not covariant! List<string>[] lsa = new List<string>[10]; This is fine public class DecimalString : Comparable<int>, Comparable<string> {... This is fine public class Comparable2<U, V> : Comparable<U>, Comparable<V> {... U and V might be the same type 8

Quiz 5: ArrayList (Java and C#) Does it work? class ArrayList<V> { private V[] storage; public ArrayList() { storage = new V[DEFAULT_SIZE]; How to make it work in Java? class ArrayList<V> { Java: cannot create an array of V C#: it s ok private V[] storage; public ArrayList() { It s a cast, but it works storage = (V[]) new Object[DEFAULT_SIZE]; } } 9

Quiz 6: Does it compile? (C#) public T Convert<T>(Object o) where T: class { if (o.gettype() == typeof (T)) { return (T) o; } else { return null; null might not be a valid value of T 10

Quiz 7: What is printed? (Java) List<Integer> l1 = new ArrayList<Integer>(); List<String> l2 = new ArrayList<String>(); Class<?> c1 = l1.getclass(); Class<?> c2 = l2.getclass(); System.out.println(c1.equals(c2)); True Both c1 and c2 are ArrayList 11

Quiz 7: What is printed (C#) List<int> l1 = new List<int>(10); List<string> l2 = new List<string>(10); Type t1 = l1.gettype(); Type t2 = l2.gettype(); Console.WriteLine(t1.Equals(t2)); False 12

Quiz 8: What happens? (Java) package ch.ethz.reflect; public class AClass { private String s; public AClass(String s) { this.s=s; public static void main(string[] args) { Class<?> c=class.forname("ch.ethz.reflect.aclass"); c.newinstance(); InstantiationException at runtime: no default constructor Compilation error: checked exceptions have to be handled 13

Quiz 8: A safe solution (Java) public static void main(string[] args) { try { Class<?> c=class.forname("ch.ethz.reflect.aclass"); Class<?>[] types = new Class[] { String.class }; Constructor<?> cons = c.getconstructor(types); Object[] a = new Object[] { "hello" }; Object newinstance = cons.newinstance(a);... } catch (Exception e) {... 14

Quiz 8: What happens? (C#) namespace Reflect { class AClass { private string s; public AClass(string s) { this.s = s; public static void Main(string[] args) { Type t = Type.GetType("Reflect.AClass"); Object o = Activator.CreateInstance(t); MissingMethodException at runtime: no default constructor No checked exceptions in C# 15

Quiz 8: A safe solution (C#) namespace Reflect { class AClass { private string s; public AClass(string s) { this.s = s; public static void Main(string[] args) { Type t = Type.GetType("Reflect.AClass"); Object[] a = { "hello" }; Object o = Activator.CreateInstance(t, a); 16

Questions? 17