#try with () vs try with {}

1 messages · Page 1 of 1 (latest)

random tide
#

Hi, is there a difference between

try(code) {other code}

and just try {code} ? thanks

crisp bearBOT
#

<@&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>.

#
TJ-Bot
Hi, is there a difference between try(code) {other code} and just try {code} ? thanks

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.

pale prism
random tide
#

in theses two case if I make a getRessource, at the end ressource will closed?

pale prism
#

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

random tide
#

finally is called after try/catch?

pale prism
#

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

random tide
#

because I have this code and I am note sur thaht the pool will be closed after

crisp bearBOT
pale prism
#

it wont

#

unless its closed somewhere after that line

#

hard to say without seeing the full code

#

from jedisPool creation to end of usage

random tide
#

and I can't just add jedisPool.getResource().subscribe(new Subscription(a), c) in () I think

pale prism
#

and ull also have to tell us what kind of type this variable is

random tide
#

so I should make a fnally?

pale prism
#

its hard to help u further without details

random tide
#

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?

pale prism
#

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

random tide
#

okay one sec

pale prism
#

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

random tide
#

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

pale prism
#

ur delaying ur own help process

#

please just share it

random tide
#

the class is not finished if u want, there is just this method

pale prism
#

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

random tide
#

It's okay I finally found what I was looking for. I'm sorry for the late awneser. thanks