try-catch-finally

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

二次信任 提交于 2019-12-01 12:47:07
问题 This question already has answers here : When finally is executed? [duplicate] (10 answers) Closed 3 months ago . 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()); } public static int fina() { int x = 0; try { x = 1; int a = 10/0; } catch (Exception e) { x = 2; return x; } finally { x = 3; } return x; } } 回答1: That's

Fastest `finally` for C++ [closed]

我与影子孤独终老i 提交于 2019-12-01 04:08:37
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . C++ so far (unfortunately) doesn't support finally clause for a try statement. This leads to speculations on how to release resources. After studying the question on the internet, although I found some solutions, I didn't get clear about their performance (and I would use Java if performance didn't

Uncaught RuntimeException and finally clause: which comes first?

前提是你 提交于 2019-12-01 03:24:15
A RuntimeException is thrown in try block without being caught, while the finally clause invokes System.exit() . public static void main(String[] args) { try { Integer.valueOf("NotANumber"); } finally { System.out.println("finally"); System.exit(0); } } The output is finally If System.exit(0) is removed from finally, then the output is finally Exception in thread "main" java.lang.NumberFormatException: For input string: "NotANumber" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:449) at java.lang.Integer.valueOf

Exiting an application gracefully?

ぐ巨炮叔叔 提交于 2019-12-01 01:28:09
问题 I have an application with a well defined Try/Catch/Finally chain that exits and executes the finally block just fine under normal conditions, however when someone prematurely hits the red X in the GUI, the program fully exists (code = 0) and the main thread's finally block isn't called. In fact, I do want the program to exit upon a click of the red-X, but what I do not want is a skipping of the finally{} block! I sort of put in the most important part of the finally block manually in the GUI

Uncaught RuntimeException and finally clause: which comes first?

做~自己de王妃 提交于 2019-11-30 22:47:44
问题 A RuntimeException is thrown in try block without being caught, while the finally clause invokes System.exit() . public static void main(String[] args) { try { Integer.valueOf("NotANumber"); } finally { System.out.println("finally"); System.exit(0); } } The output is finally If System.exit(0) is removed from finally, then the output is finally Exception in thread "main" java.lang.NumberFormatException: For input string: "NotANumber" at java.lang.NumberFormatException.forInputString

do I need to surround fileInputStream.close with a try/catch/finally block? How is it done?

≯℡__Kan透↙ 提交于 2019-11-30 20:45:22
I have the following Java Class that does one thing, fires out values from config.properties. When it comes time to close the fileInputStream , I think I read on Wikipedia that it is good to have it in a finally block. Because it honestly works just fine in try/catch block. Can you show me correction to get fileInputStream.close() in a finally section? ConfigProperties.java package base; import java.io.FileInputStream; import java.util.Properties; public class ConfigProperties { public FileInputStream fileInputStream; public String property; public String getConfigProperties(String strProperty

Throw exception from Called function to the Caller Function's Catch Block

好久不见. 提交于 2019-11-30 12:48:07
internal static string ReadCSVFile(string filePath) { try { ... ... } catch(FileNotFoundException ex) { throw ex; } catch(Exception ex) { throw ex; } finally { ... } } //Reading File Contents public void ReadFile() { try { ... ReadCSVFile(filePath); ... } catch(FileNotFoundException ex) { ... } catch(Exception ex) { ... } } Here in the above code sample, I have two functions ReadFile and ReadCSVFile. In the ReadCSVFile, I get an exception of type FileNotFoundExceptioon, which gets caught in the catch(FileNotFoundException) block. But when I throw this exception to be caught in the catch

do I need to surround fileInputStream.close with a try/catch/finally block? How is it done?

筅森魡賤 提交于 2019-11-30 05:37:09
问题 I have the following Java Class that does one thing, fires out values from config.properties. When it comes time to close the fileInputStream , I think I read on Wikipedia that it is good to have it in a finally block. Because it honestly works just fine in try/catch block. Can you show me correction to get fileInputStream.close() in a finally section? ConfigProperties.java package base; import java.io.FileInputStream; import java.util.Properties; public class ConfigProperties { public

Try-catch-finally in java

若如初见. 提交于 2019-11-30 02:58:20
In Java, will the finally block not get executed if we insert a return statement inside the try block of a try-catch-finally ? The only time a finally block will not be executed is when you call exit() before finally is reached. The exit() call will shutdown the JVM, so no subsequent line of code will be run. EDIT: This is not entirely correct. See the comments below for additional information. stacker The finally block will always execute no matter if you return, or an exception is thrown within the try block. See also section 14.19.2 Execution of try-catch-finally of the Java Language

Throw exception from Called function to the Caller Function's Catch Block

给你一囗甜甜゛ 提交于 2019-11-29 18:55:59
问题 internal static string ReadCSVFile(string filePath) { try { ... ... } catch(FileNotFoundException ex) { throw ex; } catch(Exception ex) { throw ex; } finally { ... } } //Reading File Contents public void ReadFile() { try { ... ReadCSVFile(filePath); ... } catch(FileNotFoundException ex) { ... } catch(Exception ex) { ... } } Here in the above code sample, I have two functions ReadFile and ReadCSVFile. In the ReadCSVFile, I get an exception of type FileNotFoundExceptioon, which gets caught in