#try with () vs try with {}
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
The difference between using try(code) {other code} and just try {code} lies in the scope of the code within the try block.
When you use try(code) {other code}, the code within the parentheses is run, and then the other code inside the curly braces is executed. This allows you to specify a specific block of code to be tried, followed by additional code to be executed after that specific block.
On the other hand, when you use just try {code}, any exceptions that occur within the specified code block will be caught by the catch blocks associated with that try block. This means that all of the code within the try block will be treated as a single unit and any exceptions thrown will be caught and handled accordingly.
In summary, using try(code) {other code} allows you to specify a specific block of code to be tried, followed by additional code to be executed after that specific block. Whereas using just try {code} treats all of the code within the try block as a single unit for exception handling purposes.
former is called try-with-resources
in theses two case if I make a getRessource, at the end ressource will closed?
yes
its roughly equivalent to:
AutoClosable foo = null;
try {
foo = new ...
...
} finally {
try {
if (foo != null) foo.close();
}
}
compared to
try (AutoClosable foo = ...) {
...
}
so it ensures that the resource is closed in all cases
even more importantly if u create two resources
then it can ensure that the second resource is also closed in case the first resource failed closing
which people usually overlook if they write it themselves
finally is called after try/catch?
finally is a block thats part of the try-catch construct which is always entered
its entered if the try block completed normally and also if it threw an exception
because I have this code and I am note sur thaht the pool will be closed after
Detected code, here are some useful tools:
it wont
unless its closed somewhere after that line
hard to say without seeing the full code
from jedisPool creation to end of usage
it not
and I can't just add jedisPool.getResource().subscribe(new Subscription(a), c) in () I think
and ull also have to tell us what kind of type this variable is
so I should make a fnally?
its hard to help u further without details
we are subscribing to a redis channel, c and a are string (subscription is a calss who extend jedis pub sub
but is not important I think, the important Is just being sure that connection is closed after that
if I put jedisPool.getResource().subscribe(new Subscription(a), c) in () it will work?
please just share ur full code for that file
we need to see the exact and full type of the variable
and how u create and use it
okay one sec
please just share ur full code for that file
we need to see the exact and full type of the variable
and how u create and use it
to be honest I don't finished the class it the beginn are you sure that is usefull ? I just want to close the pool
the class is not finished if u want, there is just this method
its up to u if u want my help or not. i wouldnt ask for it if i wouldnt need it in order to provide u with help
i need to see the full type of this variable, where its created and its usages
since u dont seem to really know what to share in order to give that, its best if u simply drag&drop the entire file
It's okay I finally found what I was looking for. I'm sorry for the late awneser. thanks