class: middle ### Announcements - Office Hours for Recitation Instructors. - Problem Set 2 is out after the lecture. - Exercise 2 is out this week. - Catch-Up Session I is this Saturday 10am to 12. No new material will be covered. --- class: middle, center ## CS2030
s
### Programming Methodology II --- class: middle, center ## Lecture 4 ### 3 February 2025 --- class: middle,left ### Quick Recap - Overloading - LSP - Abstract Classes - Interfaces --- class: middle,left ### Topics covered today: - Wrapper Class - Variance - Exceptions (Unit 19 to Unit 22) --- class: middle,center ## Wrapper Classes --- class: middle, wide .small[ ```Java boolean contains(Object[] array, Object obj) { for (Object curr : array) { if (curr.equals(obj)) { return true; } } return false; } ``` ] --- class: middle,left ### Wrapper Classes are Reference Types! ```Java Integer i = Integer.valueOf(4); int j = i.intValue(); ``` --- class: middle,left ### Auto-boxing and Unboxing ```Java Integer i = 4; int j = i; ``` --- class: middle,center ## Type Casting --- class: middle,wide .small[ ```Java GetAreable findLargest(GetAreable[] array) { double maxArea = 0; GetAreable maxObj = null; for (GetAreable curr : array) { double area = curr.getArea(); if (area > maxArea) { maxArea = area; maxObj = curr; } } return maxObj; } ``` ] --- class: middle,center ## Variance of Types --- class: middle,wide Let $C(T)$ be a complex type based on type $T$. We say a complex type $C$ is: - _covariant_ if $S <: T$ implies $C(S) <: C(T)$. - _contravariant_ if $T <: S$ implies $C(S) <: C(T)$. - _invariant_ if $C$ is neither covariant nor contravariant. --- class: middle,center ### Java array is covariant --- class: middle,wide ```Java Integer[] ints; Object[] objects; objects = ints; // Compiles as // Integer[] <: Object[] ints = objects; // Error ``` --- class: middle,wide ```Java Integer[] ints = new Integer[]{1, 2}; Object[] objects; objects = ints; objects[0] = "Oh dear"; ``` --- class: middle,center Let's consider writing a method that reads in a single integer value from a file. What can go wrong? --- class: middle .tiny[ ```Java fd = fopen(filename,"r"); if (fd == NULL) { fprintf(stderr, "Unable to open file. "); if (errno == ENFILE) { fprintf(stderr, "Too many opened files. Unable to open another\n"); } else if (errno == ENOENT) { fprintf(stderr, "No such file %s\n", filename); } else if (errno == EACCES) { fprintf(stderr, "No read permission to %s\n", filename); } return -1; } ``` ] --- class: middle .tiny[ ```Java scanned = fscanf(fd, "%d", &value); if (scanned == 0) { fprintf(stderr, "Unable to scan for an integer\n"); fclose(fd); return -2; } if (scanned == EOF) { fprintf(stderr, "No input found.\n"); fclose(fd); return -3; } ``` ] --- class: middle,center # Exceptions --- class: middle,center ## `try`-`catch`-`finally` --- class: middle .small[ ```Java try { // do something } catch (an exception parameter) { // handle exception } finally { // clean up code // even if no exception was thrown } ``` ] --- class: middle,center ## Exception Hierarchy --- background-image: url(figures/cs2030s-lec04/exception/exception.004.png) --- background-image: url(figures/cs2030s-lec04/exception/exception.001.png) --- class: middle,center ## Checked vs. Unchecked Exceptions --- class: middle,center ## Control Flow of Exceptions --- class: middle .small[```Java try { ... m1(); ... } catch (E1 e) { ... } catch (E2 e) { ... } finally { ... } ```] --- class: middle .tiny[```Java void m1() { ... m2(); ... } void m2() { m3(); ... } void m3() { ... m4(); } void m4() { ... } ```] --- background-image: url(figures/cs2030s-lec04/exception/exception.002.png) --- class: middle .tiny[```Java void m1() { ... m2(); ... } void m2() { m3(); ... } void m3() { ... m4(); } void m4() { ... throw new E2(); ... } ```] --- background-image: url(figures/cs2030s-lec04/exception/exception.003.png) --- class: middle,center ## Creating our own Exceptions --- class: middle,center ## Best Practices for Exception Handling --- class: middle ### Do use catching exceptions to clean up .tiny[```Java public void m2() throws E2 { try { // setup resources m3(); } catch (E2 e) { throw e; } finally { // clean up resources } } ```] --- background-image: url(figures/cs2030s-lec04/catch-them-all.png) --- class: middle ### Do NOT catch-them-all! ```Java try { // your code } catch (Exception e) { // do nothing } ``` --- class: middle ### Do NOT use Exception as a Control Flow Mechanism ```Java try { obj.doSomething(); } catch (NullPointerException e) { doTheOtherThing(); } ``` --- class: middle ### Do NOT Overreact ```Java try { // your code } catch (Exception e) { System.exit(0); } ``` --- class: middle,left ### Reminders - Play with the examples we give you :) --- class: bottom .tiny[ Version: v1.2 Last Updated: Sat Feb 01 21:58:00 +08 2025 ]