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
2 answers

C# Exception handling that won't break application

I am a green C# programmer and I'm trying to add exception handling to an application but unsure of the correct way to achieve the result I'm looking for. I'm parsing a CSV and grabbing values that are always in the same place. If a blank or…
0
votes
2 answers

Is there a way to handle error in a try-catch-finally if the error occurs after the finally block?

I was just curious. I have just written my first major code in C#, using try-catch-finally. I was wondering if there existed such a method to handle errors if there was additionally stuff to be processed after inside the finally block. Thanks.
LaDante Riley
  • 521
  • 4
  • 14
  • 26
0
votes
2 answers

Why return is not honoring the value of variable in finally block?

finally always gets executed last, so the statement x = 3 should be executed last. But, when running this code, the value returned is 2. Why? class Test { public static void main (String[] args) { System.out.println(fina()); } …
0
votes
1 answer

Release open Connection in Java 8

I have a JSP/Servlet based application, the database team is complaining about the increase in open database connection.I suspect that the connection is not closed after use. I want to make some code changes by initializing the connection string in…
0
votes
3 answers

Recursion within a try-catch-finally block: How to only call the finally block one time, on the original function call?

Let's say I have a function named foo(). Within foo() there is a try-catch-finally block. Inside the catch block, foo() is called recursively. My question is: How can I have the finally block only execute once, on the original function call? I want…
Sprag
  • 69
  • 11
0
votes
7 answers

Java: Hypothetical question about finally block

What happens if you throw an error in a finally block? Does it get handled in one of the corresponding catch clauses?
well actually
  • 11,810
  • 19
  • 52
  • 70
0
votes
1 answer

Itext5 document not closing properly

The following method is made in order to scale a pdf document to a desired format : Document document = new Document(MarginsPDFHelper.DIM_PAGE, MarginsPDFHelper.MARGIN_GEN, MarginsPDFHelper.MARGIN_GEN, MarginsPDFHelper.MARGIN_TOP,…
Ramzi G.
  • 59
  • 1
  • 10
0
votes
1 answer

How could I rework this code so as not to appear as if I am using exceptions as a form of flow-control?

I've heard it said that exceptions and try-catch blocks should not be used to flow-control, so I would like a way to rework this code to avoid that appearance. I have a method validateTrainingSets, within a neuralNetwork class that does exactly what…
Tara
  • 389
  • 3
  • 14
0
votes
0 answers

Java know in finally that exception thrown without any variable

Is there a way to know in finally that exception is thrown, without using any extra variable as below: boolean hasFailed = true; try { // code //... hasFailed = false; } finally { if (hasFailed) { // handle failure …
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0
votes
6 answers

C# - Define var outside the try block and return it

I am a newbie and trying to get back in the programming game. Sorry for my ignorance and lack of knowledge as well. I am trying to see how to fix the return type error message in the code below. I know I can define the variable outside the try…
dbss
  • 17
  • 1
0
votes
3 answers

Nested try blocks with catches after finally

I want to write a code like this: try { try { someStuffThatCausesBusinessExceptions(); } finally { try { cleanUp(); } catch (Exception e) { // I don't really care } } } catch…
Morse
  • 1,330
  • 2
  • 17
  • 27
0
votes
1 answer

Finally block not executed when using HttpClient

I'm working on an Xamarin.Forms application with MVVM. In my viewmodel I want to Get all the patients from an API. This is the code: public async Task GetAllPatients() { try { isFetchingData = true; …
Marijke
  • 153
  • 3
  • 14
0
votes
2 answers

Why didn't the code printed the line after "finally"? (Java)

I have a method with a try and catch block, and the code after the finally statement did not print ("end of a" line). The method threw an exception and I suspect this is the reason. Is the reason for the line System.out.println("end of…
Assaf
  • 1,112
  • 4
  • 14
  • 35
0
votes
0 answers

angularjs menu disappear after http call

I am using AngularJS, and when I use http call, the menu disappears if the request returns 404 not found. When the http call is ok, every thing works fine. this is my code this.fetchList = function () { var url = '/api/getlistmanager'; …
Majdi Taleb
  • 731
  • 3
  • 9
  • 26
0
votes
4 answers

Use Try-With-Resources instead of finally block when multiple lines in try block throws exception

Code Snippet : InputStream inputStream = null; try{ ExternalServiceObject object = externalService.getObject(); inputStream = object.getInputStream(); // further uses of inputStream } catch(Exception e){ throw e; } finally { …