From 544dcbfefeeffd1d9d4517663ac5d5a855871c73 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 9 May 2023 10:03:13 -0600 Subject: [PATCH] Day 2 Break 2 --- Questions.txt | 87 ++++++++++++++++++++++++++++++++ src/main/java/autoclose/Ex1.java | 20 ++++++++ src/main/java/autoclose/Ex2.java | 30 +++++++++++ 3 files changed, 137 insertions(+) create mode 100644 src/main/java/autoclose/Ex1.java create mode 100644 src/main/java/autoclose/Ex2.java diff --git a/Questions.txt b/Questions.txt index 1c8ba27..e84d3a1 100644 --- a/Questions.txt +++ b/Questions.txt @@ -304,3 +304,90 @@ D) The code compiles if the word "yield" is inserted after the colon in both the case and default E) The code compiles if the changes of both C and D are applied + +Q) Given + void doStuff() throws IOException {}; + void doStuff2() throws FileNotFoundException {}; +And: + void tryStuff() { + try { + doStuff(); + doStuff2(); + } enter code here { + // handle both exceptions + } + } +What can be inserted at "enter code here" +to provide handling of exceptions from both +methods? +A) catch (IOException | FileNotFoundException e) +B) catch (FileNotFoundException | IOException e) +C) catch (FileNotFoundException e) +D) catch (IOException e) +E) finally + +Q) Given + void doStuff() throws SQLException {}; + void doStuff2() throws FileNotFoundException {}; +And: + void tryStuff() /*...insert here...*/ { + try { + doStuff(); + doStuff2(); + } catch (SQLException | FileNotFoundException e) { + throw e; + } + } + +What is best at "...insert here..."? +A) throws Exception +B) throws SQLException +C) throws FileNotFoundException +D) throws FileNotFoundException, SQLException +E) nothing is needed, compilation succeeds as is + +Q) Given: +class AC implements AutoCloseable { + private String name; + public AC(String name) { this.name = name; } + @Override public void close() { + System.out.print("Closing " + name); + } +} +And: + var ac0 = new AC("zero"); + try ( + var ac1 = new AC("one"); + var ac2 = new AC("two"); + ac0; + ) { + } +} +What is the result? +A) Compilation fails +B) Closing zero Closing two Closing one +C) Closing zero Closing one Closing two +D) Closing two Closing one Closing zero +E) Closing one Closing two Closing zero + +Q) Given +class AC4 implements AutoCloseable { + @Override + public void close() throws Exception { + throw new SQLException(); + } +} +And: + try ( + var one = new AC4(); + var two = new AC4(); + ){ + throw new FileNotFoundException(); + } + +Which are true? +A) Compilation fails +B) A FileNotFoundException is thrown to the caller +C) An SQLException is thrown to the caller +D) close methods of resources "one" and "two" are called +E) close methods of resources "one" and "two" are not both called diff --git a/src/main/java/autoclose/Ex1.java b/src/main/java/autoclose/Ex1.java new file mode 100644 index 0000000..4fa193b --- /dev/null +++ b/src/main/java/autoclose/Ex1.java @@ -0,0 +1,20 @@ +package autoclose; +class AC implements AutoCloseable { + private String name; + public AC(String name) { this.name = name; } + @Override public void close() { + System.out.print("Closing " + name); + } +} +public class Ex1 { + public static void main(String[] args) { + var ac0 = new AC("zero"); + try ( + var ac1 = new AC("one"); + var ac2 = new AC("two"); + ac0; + ) { + } +// ac0 = null; // must be final or effectively final! + } +} diff --git a/src/main/java/autoclose/Ex2.java b/src/main/java/autoclose/Ex2.java new file mode 100644 index 0000000..3875ae5 --- /dev/null +++ b/src/main/java/autoclose/Ex2.java @@ -0,0 +1,30 @@ +package autoclose; + +import java.io.FileNotFoundException; +import java.sql.SQLException; + +class AC4 implements AutoCloseable { + @Override + public void close() throws Exception { + throw new SQLException(); + } +} + +class Q7 { + public static void main(String[] args) { + try { + doIt(); + } catch (Throwable t) { + t.printStackTrace(); + } + } + public static void doIt() throws Throwable { + try ( + var one = new AC4(); + var two = new AC4(); + ) { +// throw new FileNotFoundException(); + } + } +} +