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

Using Try-Catch-Finally to handle arithmetic exceptions

I would like to try TWO different things (both have a strong possibility of failure) and for this reason I would like to use the "finally" statement to run a "safety" just in case the first two tries both fail. Take the following example (no this is…
user725913
0
votes
1 answer

Closing Scanner/PrintStream in finally-block which are declared in try-block

I am trying to close a Scanner and a PrintStream in a finally-block, which are declared in a try-block. The problem is, if the try-block fails because of an exception, the Scanner and Printstream are never declared, resulting in an error in the…
0
votes
1 answer

In python (2.7) when using 'with open', do I need a 'finally close' block in case of error thrown (see example)

When using 'with open' if an exception occurs do I need a finally block to ensure the file is closed. e.g. try: with open(self.some_path, "r") as a_file: self.yaml_contents = yaml.load(a_file.read()) except IOError as ex: …
0
votes
2 answers

What is exactly the use of finally block? In java 7 anyways there is automatic resource management feature

And moreover instead of writing the code in finally block we can simply catch the exception in catch block and whatever resources we need to clean we can write it after the try-catch block. What is the use of that finally block then?
0
votes
1 answer

Problem with: "Variable used as a try-with-resources resource should be final or effectively final"

From my point of view, I use try with resources so I don't have to close resources manually in try block. So in case of making a FileWriter, I wouldn't have to try, then catch and close it in finally, but just do this: void m1(){ …
Stefan
  • 969
  • 6
  • 9
0
votes
2 answers

R tryCatch() - referencing return of expr() in finally?

I am trying to write a function to handle execution of batch jobs, logging errors and stats of the job results. Is there a way to reference returning value of expr block, from finally block? my_do <- function(FUN, ...){ result <- tryCatch({ …
taiyodayo
  • 331
  • 4
  • 13
0
votes
1 answer

Pycharm's "stop" does not run finally code

I am running a python project in pycharm. In the code we have a main "try-catch-finally" block e.g. try: # Some stuff like opening files and video streams except SomePossibleExceptions: # Handle possible exception finally: # Save, close and…
0
votes
4 answers

Try Catch Exception Helper

I already made a helper that returns an error message using the Exception parameter in another function.(This is an example) void func(int n){ try { // this will throw ArithmeticException if n is 0 int x = 10 / n; int…
0
votes
0 answers

Try catch block in nested foreach loop not returning correct output, c#

I know this question has been asked many times, but I cannot get the logic behind it. I have this nested foreach loop structured like this: foreach (string currentFile in csvfiles)//loops through all csv files { string fileName =…
0
votes
2 answers

C# can't avoid nested try-catch

I am pretty new to C#. I am currently writing a WebSocket application, and I need to handle the NullReferenceException when the client disconnects, as I am constantly reading data from ClientSocket. So the trouble is: When I place the second…
0
votes
0 answers

Try-catch problem in Embarcadero C++ 10.1 x64

I have a problem executing 64-bit version of my program: _XMLDoc = new TBasicXML(_owner); try { _XMLDoc->LoadFromFile(_filePath); } catch (...) { delete _XMLDoc; return "?"; } In 32-bit version, when _filePath does not exist, program…
S. Petrić
  • 80
  • 10
0
votes
2 answers

how to get the statement in finally block printed after the return statement from try and catch block?

I want the statement in the finally block to be printed after the return statement of try and catch block, but the statement in finally block always prints before this. 1 import java.io.*; 2 import java.util.*; 3 public class Division 4…
krish
  • 11
  • 1
0
votes
1 answer

PHP: try-catch-finally in loop with continue in catch

Ok, just a technical question about the code above: foreach($x as $y){ // or even a simple for() try{ a(); } catch(\Exception $e){ // just skip current iteration continue; } finally { c(); } } Since…
fudo
  • 2,254
  • 4
  • 22
  • 44
0
votes
2 answers

Java - Variable scope inside a try-catch - Contrast between most answers and an official Java tutorial

I was reading the following official guide and I've found a problem, using this two snippets of code together will lead into an error (no scope for the stmt object): Processing ResultSet Objects try { stmt = con.createStatement(); ResultSet…
Shanrya
  • 5
  • 1
0
votes
1 answer

Java finally block changes value of class variable but not return statement in Try block

I've seen this behavior explained away because it was usually an immutable String in the finally block but I don't understand why an int primitive would behave this way. "i" is not passed by value as an argument in a method. The method is…
A. J. Green
  • 93
  • 2
  • 8