try-catch

Avoid Empty Catch Blocks When Expecting Exception

倾然丶 夕夏残阳落幕 提交于 2019-12-08 20:47:49
问题 I am trying to parse dates using SimpleDateFormat . As my service takes in multiple date formats, I have adopted this approach: String[] formats = { "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSS-HH:mm", "EEE MMM dd HH:mm:ss Z yyyy"}; for (String format : formats) { try { return new SimpleDateFormat(format).parse(dateString); } catch (ParseException e) {} } return null; The rationale behind the try-catch is that if the current date format couldn't

Type of cfcatch in ColdFusion

萝らか妹 提交于 2019-12-08 19:37:50
问题 I am simply using a cftry/cfcatch block for handling any exception. Take this simple exception: <cftry> <cfset abc = 1/0> <cfcatch> <cfdump var="#cfcatch.getClass().getName()#"> <cfdump var="#isStruct(cfcatch)#"> <cfdump var="#isObject(cfcatch)#"> <cfdump var="#structKeyExists(cfcatch, 'type')#"> </cfcatch> </cftry> And the above code's output is like this: coldfusion.runtime.DivideByZeroException NO YES YES My question is: Why structKeyExists is not throwing an error as cfcatch is not of

Why close method of java.lang.AutoCloseable throws Exception, but close method of java.io.Closeable throws IOException?

房东的猫 提交于 2019-12-08 18:45:04
问题 I was reading this link for try-with-resources and it says: The close method of the Closeable interface throws exceptions of type IOException while the close method of the AutoCloseable interface throws exceptions of type Exception . But why? The close method of AutoCloseable could have also thrown IOException is there any example that support that close method of AutoCloseable must throw exceptions of type Exception 回答1: The AutoClosable interface is located in java.lang and is intended to

Catch exception thrown in foreach condition

假装没事ソ 提交于 2019-12-08 18:37:59
问题 I have a foreach loop that breaks during the loop in the condition of the foreach itself. Is there a way to try catch the item that throws the exception and then continue the loop? This will run a few times until the exception hits and then end. try { foreach(b in bees) { //exception is in this line string += b; } } catch { //error } This will not run at all because the exception is in the condition of the foreach foreach(b in bees) { //exception is in this line try { string += b; } catch { /

Java - TestNG : Why does my Assertion always passes when written within try-catch block

三世轮回 提交于 2019-12-08 17:17:05
问题 I was trying with a simple code using org.testng.Assert to assert 2 use-cases. In the first use-case I am asserting 2 unequal values which Fail correctly. But in the second use-case when I am asserting 2 unequal values within the try-catch block, the result is always returned as Pass My code is as follows: package demo; import org.testng.Assert; import org.testng.annotations.Test; public class Q43710035 { @Test public void test1() { System.out.println("Within test1"); int a = 12; int b =20;

How to get try / catch to work in erlang

ε祈祈猫儿з 提交于 2019-12-08 17:00:15
问题 i'm pretty new to erlang and i'm trying to get a basic try / catch statement to work. I"m using webmachine to process some requests and all i really want to do is parse some JSON data and return it. In the event that the JSON data is invalid, I just want to return an error msg. Here is the code I have so far. (the JSON data is invalid) to_text(ReqData, Context) -> Body = "{\"firstName\": \"John\"\"lastName\": \"Smith\"}", try decode(Body) of _ -> {"Success! Json decoded!",ReqData,Context}

Using finally instead of catch

南笙酒味 提交于 2019-12-08 16:37:22
问题 I've seen this pattern a few times now: bool success = false; try { DoSomething(); success = true; } finally { if (!success) Rollback(); } And I've been wondering: Why is this better than using catch for rollbacks? try { DoSomething(); } catch { Rollback(); throw; } What are the differences between the two ways of making sure changes are rolled back on failure? 回答1: The clear goal here is to cause Rollback to be called in the event of any error. Both code snippets accomplish this goal. The

How to keep the stacktrace when rethrowing an exception out of catch-context?

谁说胖子不能爱 提交于 2019-12-08 15:59:29
问题 TL;DR: how to raise a previously caught exception later on, while preserving the original exception's stacktrace. Since I think this is useful with the Result monad or computation expression, esp. since that pattern is often used for wrapping an exception without throwing it, here's a worked out example of that: type Result<'TResult, 'TError> = | Success of 'TResult | Fail of 'TError module Result = let bind f = function | Success v -> f v | Fail e -> Fail e let create v = Success v let

Return, inside or outside Try / catch?

扶醉桌前 提交于 2019-12-08 15:50:13
问题 In the below code, the IDE alerts me about "Missing return statement" in the last bracket. Which leads me to ask here if the return inside the try{} is ok or should be outside it. Thanks a lot. public function getFileNamesFromKeywords( array $ids, $format ) { try { if(self::$dbLink) { $ids = implode(',',$ids); $query = 'SELECT d.id, d.wfid, d.docid , k.keyword, k.value'. 'FROM keywords k'. 'INNER JOIN documents d '. 'ON k.document_id = d.id'. 'WHERE k.document_id IN ('.$ids.')'; $results =

Is there a situation when it's appropriate to use empty catch block? [duplicate]

北战南征 提交于 2019-12-08 15:30:28
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Why are empty catch blocks a bad idea? Is there any valid reason to ever ignore a caught exception Do you know any situations when an empty catch block is not the absolute evil? try { ... // What and When? ... } catch { } 回答1: There are a lot of questions on this, try to look at: Why are empty catch blocks a bad idea? From that post's accepted answer: Usually empty try-catch is a bad idea because you are