exception munging list autoclosable
(Collection<? extends AutoCloseable> c)
| 22 | * exception munging list autoclosable |
| 23 | */ |
| 24 | static public AutoCloseable closeable(Collection<? extends AutoCloseable> c) { |
| 25 | |
| 26 | for (AutoCloseable cc : c) { |
| 27 | if (cc instanceof Bracketable) ((Bracketable) cc).open(); |
| 28 | } |
| 29 | |
| 30 | return () -> { |
| 31 | List<Throwable> thrown = new ArrayList<>(); |
| 32 | c.forEach((autoCloseable) -> { |
| 33 | try { |
| 34 | autoCloseable.close(); |
| 35 | } catch (Exception e) { |
| 36 | e.printStackTrace(); |
| 37 | thrown.add(e); |
| 38 | } |
| 39 | }); |
| 40 | if (thrown.size() > 0) { |
| 41 | Exception e = new Exception(" exception(s) throw during close " + thrown); |
| 42 | e.initCause(thrown.get(0)); |
| 43 | throw e; |
| 44 | } |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * exception munging list autoclosable |