try-catch-finally

Scala Continuations - Why can't my shifted call be inside a try-catch block?

╄→гoц情女王★ 提交于 2019-12-06 05:06:26
问题 I'm new to Scala continuations, and relatively new to the scala language in general. I tried playing with Scala continuations and wrote the following code: case class MyException(msg:String) extends Exception def go:Int = reset { println("enter your input") val my_check = //try { val user_input = readLine() if (!user_input.matches("\\w+")) { throw new MyException("illegal string: " + user_input) } shift { k: (Boolean => Int) => { if (user_input == "true") { k(true) } else if (user_input ==

Throw Exception VS Return Error within a Try,Catch,Finally

核能气质少年 提交于 2019-12-05 02:16:34
问题 I'm pretty sure I already know the answer, but I'm still curious what the opinion is on handling an error within a Try,Catch,Finally block -- but when you're repeating yourself. BTW - I'm not talking about User Input - but using that for an example because it is clear and short Consider this bit of code... try { if (success) { return someSuccessMessage; } else { logError("User input not correct format"); return someErrorMessage; // repeats itself } } catch (Exception ex) { logError(ex.Message

In a finally block, can I tell if an exception has been thrown [duplicate]

痴心易碎 提交于 2019-12-04 22:12:46
Possible Duplicate: Is it possible to detect if an exception occurred before I entered a finally block? I have a workflow method that does things, and throws an exception if an error occurred. I want to add reporting metrics to my workflow. In the finally block below, is there any way to tell if one of the methods in the try/catch block threw an exception ? I could add my own catch/throw code, but would prefer a cleaner solution as this is a pattern I'm reusing across my project. @Override public void workflowExecutor() throws Exception { try { reportStartWorkflow(); doThis(); doThat();

Scala Continuations - Why can't my shifted call be inside a try-catch block?

允我心安 提交于 2019-12-04 10:28:17
I'm new to Scala continuations, and relatively new to the scala language in general. I tried playing with Scala continuations and wrote the following code: case class MyException(msg:String) extends Exception def go:Int = reset { println("enter your input") val my_check = //try { val user_input = readLine() if (!user_input.matches("\\w+")) { throw new MyException("illegal string: " + user_input) } shift { k: (Boolean => Int) => { if (user_input == "true") { k(true) } else if (user_input == "false") { k(false) } else { // don't even continue 0 } } } } // catch { // case MyException(msg) =>

Does a finally block always get executed in Java?

心已入冬 提交于 2019-12-04 05:34:41
问题 Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is? try { something(); return success; } catch (Exception e) { return failure; } finally { System.out.println("I don't know if this will get printed out"); } 回答1: Yes, finally will be called after the execution of the try or catch code blocks. The only times finally won't be called are: If you invoke System.exit() If you invoke Runtime.getRuntime().halt(exitStatus) If the JVM

Throw Exception VS Return Error within a Try,Catch,Finally

南笙酒味 提交于 2019-12-03 19:05:57
I'm pretty sure I already know the answer, but I'm still curious what the opinion is on handling an error within a Try,Catch,Finally block -- but when you're repeating yourself. BTW - I'm not talking about User Input - but using that for an example because it is clear and short Consider this bit of code... try { if (success) { return someSuccessMessage; } else { logError("User input not correct format"); return someErrorMessage; // repeats itself } } catch (Exception ex) { logError(ex.Message); return someErrorMessage; // repeats itself } Say we have a function, that if it fails we want to

Try-catch-finally in java

怎甘沉沦 提交于 2019-12-03 18:57:04
问题 In Java, will the finally block not get executed if we insert a return statement inside the try block of a try-catch-finally ? 回答1: 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. 回答2: The finally block will always execute no matter if you return, or an exception is thrown

Is finally block really necessary for the clean up code (like closing streams)?

懵懂的女人 提交于 2019-12-03 14:02:19
I am very confused as to why do I need to need to put the clean-up code like closing streams in a finally block. I've read that the code in finally block will run no matter what (whether there's an exception); and after the finally block runs, the rest of the method continues. My question is: if the rest of the method has to continue then why don't I put the clean-up code after my try/catch block in a function? The finally block will always run if an uncaught exception is thrown, but the rest of the code in the method will be skipped. So if you put clean-up code after the finally block, it won

Using Exception Handling versus NSError in Cocoa Apps

人盡茶涼 提交于 2019-12-03 12:17:51
问题 Hey all. I've been reading up on Apple's suggestions for when/where/how to use NSError versus @try/@catch/@finally. Essentially, my impression is that Apple thinks it best to avoid the use of exception handling language constructs except as a mechanism for halting program execution in unexpected error situations (maybe someone could give an example of such a situation?) I come from Java, where exceptions are the way to go when one wants to handle errors. Admittedly, I'm still in the Java

Close file in finally block doesn't work

白昼怎懂夜的黑 提交于 2019-12-03 11:44:29
try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line = null; } catch (FileNotFoundException fnf) { fnf.printStackTrace(); } finally { fr.close(); } The fr.close() shows an error: fr cannot be resolved I had read that closing a file in the finally block is a good practice. What is that am doing wrong? The variable fr only has scope within the try block. It is out of scope in the finally block. You need to declare it before the try block: FileReader fr = null; try { fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line =