Questions tagged [try-catch-finally]

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

435 questions
0
votes
4 answers

What does C# throw and finally means in lay men terms?

Say you are running a program, and it meets a "THROW" statement... what happens? Will the program stop? Will it continue? And what's "FINALLY" for? Please I appreciate an explanation in simple words
user1034912
  • 2,153
  • 7
  • 38
  • 60
0
votes
6 answers

Finally block to behave differently

I have a condition like this String str = null; try{ ... str = "condition2"; }catch (ApplicationException ae) { str = "condition3"; }catch (IllegalStateException ise) { str = "condition3"; }catch (Exception e) { str =…
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190
0
votes
1 answer

java: Exceptions: always reach finally?

Possible Duplicate: Does a finally block always run? let's imagine the following scenario: public void myMethod() throws MyException try { // do something // an Exception (for example an individual written MyException which extends…
nano7
  • 2,455
  • 7
  • 35
  • 52
-1
votes
1 answer

try/catch/finally Exception with System.exit(0)

try { fruit fr = (fruit) p; System.exit(0); } catch (Exception e) { System.out.println("not the right object"); } finally { System.out.println("finablock"); } why in this case System.exit(0) didn't terminate the action and…
-1
votes
2 answers

Java: finally-block seems to be executed twice when InterruptedException is thrown in catch block

In the following code: void foo() throws InterruptedException { try { f1(); } catch (InterruptedException e) { f2(); throw e; } finally { f3(); //breakpoint hit twice } } When…
A.G.
  • 2,037
  • 4
  • 29
  • 40
-1
votes
2 answers

Does GOTO in CATCH block in c# execute FINALLY?

Pretty simple question: int retryAttempts = 3; retry: try { await getSemaphore(); throw new Exception(); } catch { if (0 > retryAttempts--) { await Task.Delay(5000); goto retry; } return; } finally { …
André Reichelt
  • 1,484
  • 18
  • 52
-1
votes
2 answers

Close a resource in a Finally block from a try catch with resources

I have in my code the next: try (DockerClient client = DefaultDockerClient.fromEnv().connectTimeoutMillis(TimeUnit.SECONDS.toMillis(3)).build()) { //some code here }catch (DockerCertificateException e) { log.warn("Failed to connect Docker…
rasilvap
  • 1,771
  • 3
  • 31
  • 70
-1
votes
1 answer

Try/Catch with an SQL connection

This is a follow up question to this question: Correct use of Try Catch for the SQL connection in C# when you write a code like: using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); …
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
-1
votes
5 answers

Can we avoid finally block if we do resource cleanup in both try and catch together?

Can we avoid finally block if we do resource cleanup in both try and catch together? E.g. Can we do something like this and not use finally? try{ // Some statements db.close(); }catch(Exception e){ db.close(); } Will it work as expected? If Yes…
Nadeem
  • 121
  • 2
  • 13
-1
votes
1 answer

Error Handling in Python3 (os.system)

In Python, I am calling sub files from main python file. In all sub python files, I have included try and except block. In main file, I need the sub files to be executed in the order I have mentioned below. Is there a way to stop executing…
Sanjay
  • 328
  • 1
  • 3
  • 13
-1
votes
2 answers

Why value from inside of "try" is printed during catching the exception?

I have code like this: class ExceptionTest{ public Integer divide(int a, int b) { try { return a/b; }finally { System.out.println("Finally"); } } } public class Three…
Gregory
  • 65
  • 6
-1
votes
1 answer

How to find out if my Windows timer is running perfect and not causing any memory leak

I am calling two timers in one WinForm for different processes, both are using the same try-catch format. It works fine for the first few days, then starts to become slow. I can see some load on the server. Is my timer event correct? Is this event…
Mdyahiya
  • 167
  • 3
  • 15
-1
votes
2 answers

Except Finally - Operator

i have an curious Question regarding except Block, especially finally: During the first Block of Code finally works as it is supposed to work, meanwhile in second which is minimaly different from the first one it always gives you an…
Roman
  • 113
  • 1
  • 1
  • 6
-1
votes
1 answer

What is the purpose of finally if it always executes in java?

Consider the following Java snippets: try{ //Some code to tryout } catch(Exception e){ //Catch if there is an exception } finally{ //SNIPPET1 that is always executed } The above snippet is essentially equal to try{ //Some code to…
router
  • 582
  • 5
  • 16
-1
votes
2 answers

finally expression in python

Running the following code: click here I got the next output: 3 ok 6 ok oops ok ok Boom I don't understand why does he prints the bolded ok? he doesn't even enter the loop. I would like to get in-depth understanding of how exceptions and finally in…