try-with-resources

What on earth is “Self-suppression not permitted” and why is Javac generating code which results in this error?

╄→尐↘猪︶ㄣ 提交于 2021-02-08 12:21:58
问题 This new Java 7 try-with-resources construct is quite nice. Or at least, it was nice until an exception came along and ruined my day. I've finally managed to boil it down to a reproducible test which uses nothing but JUnit+jMock. @Test public void testAddSuppressedIssue() throws Exception { Mockery mockery = new Mockery(); final Dependency dependency = mockery.mock(Dependency.class); mockery.checking(new Expectations() {{ allowing(dependency).expectedCall(); allowing(dependency).close(); }});

Is it possible to use try with resources along with an input stream?

て烟熏妆下的殇ゞ 提交于 2021-02-08 11:47:19
问题 When implementing try with resources I am creating a Scanner object via Scanner sc = new Scanner(System.in) within () of the try statement. In the try block, I am prompting the user to enter a numeric value, reading it via sc.nextLine() and utilizing parseDouble to convert it to a method. I utilize a do-while loop to re-prompt the user to enter a value if an invalid value was entered initially. However, if the user enters an invalid value, the input stream closes, NumberFormatException is

If it safe to return an InputStream from try-with-resource [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 07:55:38
问题 This question already has answers here : Try-with-resources and return statements in java (4 answers) Closed 5 years ago . Is it safe to return an input stream from a try-with-resource statement to handle the closing of the stream once the caller has consumed it? public static InputStream example() throws IOException { ... try (InputStream is = ...) { return is; } } 回答1: It's safe, but it will be closed, so I don't think it is particularly useful... (You can't reopen a closed stream.) See

If it safe to return an InputStream from try-with-resource [duplicate]

核能气质少年 提交于 2021-02-05 07:55:10
问题 This question already has answers here : Try-with-resources and return statements in java (4 answers) Closed 5 years ago . Is it safe to return an input stream from a try-with-resource statement to handle the closing of the stream once the caller has consumed it? public static InputStream example() throws IOException { ... try (InputStream is = ...) { return is; } } 回答1: It's safe, but it will be closed, so I don't think it is particularly useful... (You can't reopen a closed stream.) See

Using resources of try-with-resources outside try

时光怂恿深爱的人放手 提交于 2021-01-28 03:50:40
问题 I'm using SocketChannel to send message between a server and a client. Once a client connects with a server, the server opens the InputStreams and OutputStream in a try-with-resources try, to receive messages from the client and send messages to the client, like so: try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {} I want to access out outside of the try. The try contains a while loop that

Using resources of try-with-resources outside try

好久不见. 提交于 2021-01-28 02:44:07
问题 I'm using SocketChannel to send message between a server and a client. Once a client connects with a server, the server opens the InputStreams and OutputStream in a try-with-resources try, to receive messages from the client and send messages to the client, like so: try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {} I want to access out outside of the try. The try contains a while loop that

JDBC with try with resources

匆匆过客 提交于 2021-01-27 17:18:05
问题 I am trying to create a centralized class that connects and returns the ResultSet of a SQL query so that I don't always have to create a new connection every time I am trying to get a query. I am using the try-with-resources , however, I am getting a compile-time error whenever I use the try-with-resources and I don't know why? public class JDBC { // logger declaration is omitted private static final String dbURL = "jdbc:oracle:"; private static final String userName = "blah"; private static

Is the close method on a try-with-resources idiom not called if a constructor throws an exception?

半世苍凉 提交于 2021-01-22 04:51:23
问题 I have a base class Base and a child class Child which extends it. Base implements java.lang.AutoCloseable . Let's suppose that the constructor for Child throws a Foo . Now consider try (Base c = new Child()){ /*Some code*/ } catch (final Foo e){ /*Some more code*/ } Is the Base#close method called if the exception is thrown? It is not on my machine, but is this something that the JLS has standardised? 回答1: Yes, close won't be called. This is specified in the JLS section 14.20.3: Resources

Why does try-with-resource require a local variable?

萝らか妹 提交于 2020-08-17 23:47:17
问题 With reference to my question Any risk in a AutoCloseable wrapper for java.util.concurrent.locks.Lock?, I am wondering why trh try-with-resource require a named local variable at all. My current usage is as follows: try (AutoCloseableReentrantReadWiteLock.Lock l = _lock.writeLock()) { // do something } The variable l is unused inside the try block and only pollutes the namespace. From what I can remember the analogous C# using statement does not require a local named variable. Is there any

Why does try-with-resource require a local variable?

◇◆丶佛笑我妖孽 提交于 2020-08-17 23:46:20
问题 With reference to my question Any risk in a AutoCloseable wrapper for java.util.concurrent.locks.Lock?, I am wondering why trh try-with-resource require a named local variable at all. My current usage is as follows: try (AutoCloseableReentrantReadWiteLock.Lock l = _lock.writeLock()) { // do something } The variable l is unused inside the try block and only pollutes the namespace. From what I can remember the analogous C# using statement does not require a local named variable. Is there any