try-catch

Is a JavaScript try-catch ignoring an expected occasional error bad practice?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 03:32:04
问题 In JavaScript is it wrong to use a try-catch block and ignore the error rather than test many attributes in the block for null? try{ if(myInfo.person.name == newInfo.person.name && myInfo.person.address.street == newInfo.person.address.street && myInfo.person.address.zip == newInfo.person.address.zip) { this.setAddress(newInfo); } } catch(e) {} // ignore missing args 回答1: If you expect a particular condition, your code will be easier to maintain if you explicitly test for it. I would write

How do I catch Selenium Errors using WebDriverJS

亡梦爱人 提交于 2019-12-07 03:19:46
问题 So I am using the JavaScript implementation of Selenium, WebDriverJS. As with any web browser automation the biggest hurdle is getting the code slow down long enough for page elements to load. My solution is this: for each element I want to interact with I have a code block like this xpath = "//div[@id='gs_lc0']/input[@id='gbqfq']" driver.wait(function(){ return waitForElement(xPath,driver); }); try{ element = driver.findElement(webdriver.By.xpath(xPath)); }catch(e){ console.log("Wait

loss exception in block catch

廉价感情. 提交于 2019-12-07 02:35:24
问题 I run this code: public class User { public static void main(String args[]) { int array[] = new int[10]; int i = 1; try { System.out.println("try: " + i++); System.out.println(array[10]); System.out.println("try"); } catch (Exception e) { System.out.println("catch: " + i++); System.out.println(array[10]); System.out.println("catch"); } finally { System.out.println("finally: " + i++); Object o = null; o.hashCode(); System.out.println("finally"); } } } Result: try: 1 catch: 2 finally: 3

Implicitly-Typed Variables in try…catch

蓝咒 提交于 2019-12-07 02:30:24
问题 I like using implicit typing for almost everything because it's clean and simple. However, when I need to wrap a try...catch block around a single statement, I have to break the implicit typing in order to ensure the variable has a defined value. Here's a contrived hypothetical example: var s = "abc"; // I want to avoid explicit typing here IQueryable<ABC> result = null; try { result = GetData(); } catch (Exception ex) { } if (result != null) return result.Single().MyProperty; else return 0;

Catch a fatal exception and continue

人盡茶涼 提交于 2019-12-07 02:04:30
问题 I know, that by its very definition, a fatal exception is supposed to kill the execution, and should not be suppressed, but here's the issue. I'm running a script that scrapes, parses and stores in a DB about 10,000 pages. This takes a couple of hours, and in rare cases (1 in 1000) a page fails parsing and throws a fatal exception. Currently, I'm doing this: for ($i=0;$i<$count;$i++) { $classObject = $classObjects[$i]; echo $i . " : " . memory_get_usage(true) . "\n"; $classDOM = $scraper-

Invoke interrupt from R code

隐身守侯 提交于 2019-12-07 01:56:52
问题 I have a generic function to catch all exceptions included in my package logR::tryCatch2 defined as: tryCatch2 <- function(expr){ V=E=W=M=I=NULL e.handler = function(e){ E <<- e NULL } w.handler = function(w){ W <<- c(W, list(w)) invokeRestart("muffleWarning") } m.handler = function(m){ attributes(m$call) <- NULL M <<- c(M, list(m)) } i.handler = function(i){ I <<- i NULL } V = suppressMessages(withCallingHandlers( tryCatch(expr, error = e.handler, interrupt = i.handler), warning = w.handler,

Delphi Exception handling problem with multiple Exception handling blocks

早过忘川 提交于 2019-12-07 01:09:34
问题 I'm using Delphi Pro 6 on Windows XP with FastMM 4.92 and the JEDI JVCL 3.0. Given the code below, I'm having the following problem: only the first exception handling block gets a valid instance of E. The other blocks match properly with the class of the Exception being raised, but E is unassigned (nil). For example, given the current order of the exception handling blocks when I raise an E1 the block for E1 matches and E is a valid object instance. However, if I try to raise an E2, that

PHP web application when to use try/catch

送分小仙女□ 提交于 2019-12-06 23:45:48
问题 I am using PHP with CodeIgniter framework. I read some articles that stating using try/catch methods is in bad practice. I understand to use logging in development when a potential error can occur and let the actual error happen then log it using log_message('level', 'message') But when in deployment you want to suppress and handle any errors that arise. Should I be using try/catch blocks, for instance in something like... try { $this->data['important'] = $this->Test_Model->do_something($data

Could someone explain this try/catch alternative in bash?

ぐ巨炮叔叔 提交于 2019-12-06 18:26:19
问题 So I found out that bash does not handle exceptions (there is no try/catch). For my script, I would like to know if a command was successful or not. This is the part of my code right now: command = "scp -p$port $user:$password@$host:$from $to" $command 2>/dev/null if (( $? == 0 )); then echo 'command was successful' else echo 'damn, there was an error' fi The things I don't understand are: line 3, why do I have to put the 2 behind the $command ? line 5, what exactly is it with this $ ? 回答1: $

Try/catch oneliner available?

橙三吉。 提交于 2019-12-06 17:14:02
问题 Just as you can convert the following: var t; if(foo == "bar") { t = "a"; } else { t = "b"; } into: t = foo == "bar" ? "a" : "b"; , I was wondering if there is a shorthand / oneline way to convert this: var t; try { t = someFunc(); } catch(e) { t = somethingElse; } Is there a method of doing this in a shorthand way, preferably an oneliner? I could, of course, just remove the newlines, but I rather mean something like the ? : thing for if . Thanks. 回答1: You could use the following function and