try-finally

java 6 IO - wrapped streams closing [closed]

爷,独闯天下 提交于 2019-12-02 14:23:53
Consider : public static void read(String filename) throws IOException { String charsetName = "UTF-8"; InputStream file = new FileInputStream(filename); // say no problem InputStreamReader reader = new InputStreamReader(file, charsetName); BufferedReader buffer = new BufferedReader(reader); try { buffer.readLine(); } finally { try { buffer.close(); } catch (IOException e) { // report at least e.printStackTrace(); } } } If new InputStreamReader(file, charsetName) throws UnsupportedEncodingException , the buffer.close(); line will never be called. The alternative is extra verbose : InputStream

how the Try catch finally block is executed by JVM

天大地大妈咪最大 提交于 2019-12-02 00:29:23
问题 According to Java Language Specification, Section §14.20.2 A try statement with a finally block is executed by first executing the try block. Then there is a choice: If execution of the try block completes normally, then the finally block is executed, and then there is a choice: If the finally block completes normally, then the try statement completes normally. If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S If I interpret it

Understanding the 'finally' block

寵の児 提交于 2019-12-01 05:58:49
I've written seven test cases for understanding the behavior of the finally block. What is the logic behind how finally works? package core; public class Test { public static void main(String[] args) { new Test().testFinally(); } public void testFinally() { System.out.println("One = " + tryOne()); System.out.println("Two = " + tryTwo()); System.out.println("Three = " + tryThree()); System.out.println("Four = " + tryFour()); System.out.println("Five = " + tryFive()); System.out.println("Six = " + trySix()); System.out.println("Seven = " + trySeven()); } protected StringBuilder tryOne() {

Understanding the 'finally' block

℡╲_俬逩灬. 提交于 2019-12-01 04:12:28
问题 I've written seven test cases for understanding the behavior of the finally block. What is the logic behind how finally works? package core; public class Test { public static void main(String[] args) { new Test().testFinally(); } public void testFinally() { System.out.println("One = " + tryOne()); System.out.println("Two = " + tryTwo()); System.out.println("Three = " + tryThree()); System.out.println("Four = " + tryFour()); System.out.println("Five = " + tryFive()); System.out.println("Six =

How to correctly write Try..Finally..Except statements?

久未见 提交于 2019-11-30 06:01:06
Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin Screen.Cursor:= crHourGlass; Obj:= TSomeObject.Create; try // do something finally Obj.Free; end; Screen.Cursor:= crDefault; end; if there was an error happening in the // do something section, the TSomeObject that was created I assume will not be freed and the Screen.Cursor will still be stuck as an Hour Glass, because the code was broke before getting to those lines? Now unless I am mistaking, an Exception statement should be in place to deal with any such occurence of an error,

How to determine if an exception was raised once you're in the finally block?

天涯浪子 提交于 2019-11-30 02:42:23
Is it possible to tell if there was an exception once you're in the finally clause? Something like: try: funky code finally: if ???: print('the funky code raised') I'm looking to make something like this more DRY: try: funky code except HandleThis: # handle it raised = True except DontHandleThis: raised = True raise else: raised = False finally: logger.info('funky code raised %s', raised) I don't like that it requires to catch an exception, which you don't intend to handle, just to set a flag. Since some comments are asking for less "M" in the MCVE, here is some more background on the use-case

Difference between try-finally and try-catch

倾然丶 夕夏残阳落幕 提交于 2019-11-29 20:05:37
What's the difference between try { fooBar(); } finally { barFoo(); } and try { fooBar(); } catch(Throwable throwable) { barFoo(throwable); // Does something with throwable, logs it, or handles it. } I like the second version better because it gives me access to the Throwable. Is there any logical difference or a preferred convention between the two variations? Also, is there a way to access the exception from the finally clause? tangens These are two different things: The catch block is only executed if an exception is thrown in the try block. The finally block is executed always after the

FURTHER CLARIFICATION: How to correctly write Try..Finally..Except statements?

久未见 提交于 2019-11-29 08:48:37
RE: How to correctly write Try..Finally..Except statements? I'm still confused by the OP's original question. Specifically, the last line of the procedure (outside of the try..finally..end) that reads "Screen.Cursor:=crDefault". My understanding is that any exceptions raised inside a try..except|finally..end block WILL execute the code after the "end" of the "try". procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin Screen.Cursor := crHourGlass; Obj := TSomeObject.Create; try // do something finally Obj.Free; end; Screen.Cursor := crDefault; end; In the above example, I

Stack overflow error handling in finally block

核能气质少年 提交于 2019-11-29 07:36:57
I have a program in java, which runs infinite times. Program code: void asd() { try { //inside try block System.out.println("Inside try !!!"); asd(); } finally { //inside finally System.out.println("Inside finally !!!"); asd(); } } OUTPUT : this program runs infinitely, by constantly printing both the sysouts. My question : At some point, it starts throwing StackOverflowErrors from the try block and so it reaches the finally block, where we are again calling this function recursively. But how is the recursive function in the finally block being executed as we already facing a

How to correctly write Try..Finally..Except statements?

青春壹個敷衍的年華 提交于 2019-11-29 05:13:34
问题 Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin Screen.Cursor:= crHourGlass; Obj:= TSomeObject.Create; try // do something finally Obj.Free; end; Screen.Cursor:= crDefault; end; if there was an error happening in the // do something section, the TSomeObject that was created I assume will not be freed and the Screen.Cursor will still be stuck as an Hour Glass, because the code was broke before getting to those lines? Now unless I