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
4
votes
1 answer
Return from try block in try finally block
I have two code snippets in both I return from try and have finally block too.The first one works fine and prints from finally too and later gives compile time error at line marked line1.
1st snippet
class abc {
public static void main(String…

Vishal
- 296
- 3
- 11
4
votes
1 answer
SonarQube, jump statements in finally block (squid:S1143)
I know, jump statements in finally block should not be used. In this simple example 'break' is used to break the 'switch'. SonarQube (5.6.3) with
sonar-java 4.5.0.8398 reports an issue on:
"Jump statements should not occur in "finally" blocks…

Tester
- 173
- 1
- 10
4
votes
2 answers
finally statement doesn't take effect in a thread
According to the official python documentation, "finally" statement will always be executed, and thus is usually used for clean-up operations.
If "finally" is present, it specifies a ‘cleanup’ handler. The "try" clause is executed, including any…

Charlie Lam
- 111
- 7
4
votes
2 answers
C# How do I do a Try Catch Finally without a bool to free up resources?
I'm trying to do a try-catch-finally so that if the mainLog was successfully created, but an exception was thrown after that, it will be disposed of properly. However, if mainLog was not successfully created and there exists a mainLog.Dipose()…

the_endian
- 2,259
- 1
- 24
- 49
4
votes
8 answers
throw-catch logic
try
{
try
{
throw new Exception("From Try");
}
catch
{
throw new Exception("From Catch");
}
finally
{
throw new Exception("From Finally");
}
}
catch (Exception ex)
{
…

DxCK
- 4,402
- 7
- 50
- 89
4
votes
2 answers
How to write following code with Conditional(Ternary) Operator?
I am just playing with my code. The code in if else block can be written with conditional operator (? :).
how to write following code with Conditional Operator.
import com.itextpdf.text.Document;
public class TEst {
public static void…

2787184
- 3,749
- 10
- 47
- 81
4
votes
3 answers
How should I replicate the functionality of C#'s 'using' statement in Java?
I'm converting some C# code to Java and it contains the using statement. How should I replicate this functionality in Java? I was going to use a try, catch, finally block but I thought I'd check with you guys first.

James
- 2,306
- 1
- 22
- 23
4
votes
1 answer
Open and close db with Swift + FMDB
What is the recommended way to open and close a sqlite db connection with Swift + FMDB?
I'm following this tutorial which suggests that you should open and close a database like:
let db = FMDatabase(path: databasePath as String)
if db.open() {
…

Steven Wexler
- 16,589
- 8
- 53
- 80
4
votes
1 answer
PHP finally block aborts on autoload
I have a problem with finally blocks and autoload. I am using PHP 5.5.9. Here is a minimal example:

gexicide
- 38,535
- 21
- 92
- 152
4
votes
4 answers
Execution of statements after try/catch block containing return
There are three cases to be considered :
Case 1:
public class Test {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
return;
…

webspider
- 67
- 1
- 6
4
votes
3 answers
Explain "finally"'s use in try-catch-finally blocks
I read that finally key make a try-catch block final work, even function throw exception or not. But I wonder what is different if I don't put a code inside a finally block (like Function_2 below), which is the way I'm using to coding. Thank…

NoName
- 7,940
- 13
- 56
- 108
4
votes
1 answer
Could finally block get skipped due to GC in a threaded situation?
Say that (possible on a separate thread) I am running some method ClassA.foobar().
Inside that method is a try, (possibly catch), finally block.
Now if the last reference to this ClassA object (or to this thread) is lost when execution is still in…

AnorZaken
- 1,916
- 1
- 22
- 34
4
votes
1 answer
How to detect whether an Excel workbook is closed (using Interop in C#)?
I'm working on a C# project that uses Microsoft.Office.Interop.Excel.Application to read values from an Excel file:
try {
Application xlApp = new Application();
Workbook workbook = xlApp.Workbooks.Open(filename)
// ... Load Excel values…

Steve Chambers
- 37,270
- 24
- 156
- 208
4
votes
1 answer
Spring can commit Transaction in finally block with RunTimeException in try block
The project used Spring + Hibernate
Sample code:
public void method(){
try{
dao.saveA(entityA);
//condition may be throw RuntimeException;
dao.saveB(entityB);
}catch(RuntimeException e){
throw e;
…

ChanceLai
- 78
- 1
- 8
4
votes
4 answers
How is exception handled in transactional context with finally block?
If i have transactional method like the one below, when is the finally block executed in case of transaction commit and rollback? For example if "persist some entity in database with hibernate" throws some hibernate exception which is true?
1)…

user358448
- 1,127
- 3
- 20
- 39