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.
Questions tagged [try-catch-finally]
435 questions
3
votes
5 answers
Is the finally block pointless?
I'm teaching myself C# and I am up to studying the try, catch and finally. The book I'm using is discussing how the finally block runs regardless of whether or not the try block is successful. But, wouldn't code that is written outside of the catch…

Tommy
- 69
- 4
3
votes
7 answers
C#: why bother having a 'finally' clause?
Possible Duplicate:
Why use finally in C#?
In C#, what is the point of having a finally clause?
eg.
try {
// do something
}
catch (Exception exc)
{
// do something
}
// do something
Won't the code at the end execute…

CJ7
- 22,579
- 65
- 193
- 321
3
votes
2 answers
Use tryCatch with a while loop in R
I am trying to implement tryCatch with a while loop in R, but have been running into issues. I have tried to implement a number of the proposed solutions (which revolve around for loops), but with no success.
Essentially I am querying an API with R…

Jack Power
- 303
- 1
- 3
- 14
3
votes
4 answers
try-catch-finally block in java
As per my understanding, I want to follow the best practice for releasing the resources at the end to prevent any connection leaks. Here is my code in HelperClass.
public static DynamoDB getDynamoDBConnection()
{
try
{
dynamoDB =…

Chandz
- 1,173
- 3
- 18
- 34
3
votes
2 answers
Java finally return, and strange bytecode
I saw one Java puzzle problem about finally, and return
int i = 0;
try {
return i;
} finally {
i++;
}
what's return value of this function, I know this will return 0, and I test another code
StringBuffer sb = new…

regrecall
- 521
- 1
- 6
- 20
3
votes
4 answers
return statement - finally block does not complete normally
Similar question has been asked here. But that does not provide answer.
try {
object = (Dev)Class.forName("Dev").newInstance();
} catch (Exception e)
{
throw new RuntimeException("Devis not available");
}
finally
…

codingenious
- 8,385
- 12
- 60
- 90
3
votes
1 answer
Why doesn't finally block execute after playframework renderbinary?
I was recently surprised to find that the finally block in this play framework controller action code only got called after an Exception, but never when the call actually succeeded.
try {
InputStream is = getInputStreamMethod();
…

Jeremy Goodell
- 18,225
- 5
- 35
- 52
3
votes
4 answers
Programming without "finally"
I don't have the required version of PHP that supports finally, so I am wondering if this:
try {
work();
} catch (Exception $e) {
cleanup();
throw $e;
}
cleanup();
is exactly the same as
try {
work();
} finally {
cleanup();
}

uk4321
- 1,028
- 8
- 18
3
votes
3 answers
Confused with try-catch-finally issue?
I tried to figure out execution order of try-catch-finally in java. I thought execution order should be
try
catch (if error occurred/ exception caught)
finally (whether exception caught or not)
But I am confused with the result of the following…

Ruchira Gayan Ranaweera
- 34,993
- 17
- 75
- 115
3
votes
4 answers
Python Error Handling: finally vs. new line dedented
When using try and except for error handling, does it matter whether you explicitly call finally or if you simply go to a new line that is dedented from the exception code? For example, is there any circumstance under which the two functions below…

Michael
- 13,244
- 23
- 67
- 115
3
votes
1 answer
Try-Catch Error Objective C
I am trying to get Captions from a given instagram picture, however if there is no caption the app throws an exception and crashes. How would I implement @try and @catch to do this. Here is what i have so far:
@try {
RNBlurModalView *modal =…

Prad
- 469
- 1
- 8
- 28
3
votes
3 answers
return statement in finally block
In MDN its stated,
If the finally block returns a value, this value becomes the return value of the entire try-catch-finally production, regardless of any return statements in the try and catch blocks:
So I try to execute the following…

Genghis Khan
- 970
- 2
- 11
- 21
3
votes
3 answers
Finally Clause in SQL Server Transaction? Something that will execute irrespective of success or failure?
In SQL Server, is there something similar to finally clause in try..catch... block of c# ?
I mean, I am using BEGIN TRAN, END TRAN, COMMIT TRAN, ROLLBACK TRAN etc in a SQL Server transaction and want a section or some set of actions that needs to…

Programmer
- 61
- 2
- 3
- 5
3
votes
6 answers
Why would one choose to use the finally statement instead of a catch statement? (Java)
I'm relatively new to Java and I'm still trying to understand the fundamentals. I have been learning about exception handling in the form of try-catch statements. These are fine and I understand how and why I should make use of them. The thing that…
user843337
3
votes
2 answers
Why to set an object to Nothing in the Finally block?
In this VB.NET code:
Dim o as SomeClass
Try
o = new SomeClass
'call some method on o here
Catch(...)
...
Finally
o = Nothing
End Try
Why is there a need to set o to Nothing? What if i don't set it to Nothing in the Finally block? What i…

badmaash
- 4,775
- 7
- 46
- 61