js try catch finally question# Programming - 葵花宝典l*x2016-05-06 07:051 楼本来打算周末去看电影的。looper 或者 hotel tensyvalnia两个的风评都不好。:(
s*w2016-05-06 07:052 楼my dummy code, try to find values from file, then return it.try {parse string from fileif find pattern, throw "found it"} catch (e) {if(e !== "found it") throw e} finally {res.json("pattern1":v1, "pattern2":v2});}问题是,假如文件中没找到,应该如何送一个空的json回去?第二个throw处 (inside catch )应该如何处理?many thanks !
n*t2016-05-06 07:054 楼我去 。。。这么喜欢 throw 啊【在 s*****w 的大作中提到】: my dummy code, try to find values from file, then return it.: try {: parse string from file: if find pattern, throw "found it": } catch (e) {: if(e !== "found it") throw e: } finally {: res.json("pattern1":v1, "pattern2":v2});: }: 问题是,假如文件中没找到,应该如何送一个空的json回去?
l*n2016-05-06 07:055 楼同学,finally是无论如何都执行的。【在 s*****w 的大作中提到】: my dummy code, try to find values from file, then return it.: try {: parse string from file: if find pattern, throw "found it": } catch (e) {: if(e !== "found it") throw e: } finally {: res.json("pattern1":v1, "pattern2":v2});: }: 问题是,假如文件中没找到,应该如何送一个空的json回去?
s*w2016-05-06 07:058 楼这个有些疑问,1. finally不是很推崇的吗?2. throw e 后面怎么处理?不会写多谢!【在 f*******t 的大作中提到】: ...: } catch (e) {: if (e == "found it") {: res.json("");: return;: }: throw e;: }: 别用finally
b*i2016-05-06 07:059 楼a={""};try {parse string from fileif find pattern, throw "found it"} catch (e) {if(e !== "found it") throw ea={"pattern1":v1, "pattern2":v2};} finally {res.json(a);}【在 s*****w 的大作中提到】: my dummy code, try to find values from file, then return it.: try {: parse string from file: if find pattern, throw "found it": } catch (e) {: if(e !== "found it") throw e: } finally {: res.json("pattern1":v1, "pattern2":v2});: }: 问题是,假如文件中没找到,应该如何送一个空的json回去?
l*n2016-05-06 07:0510 楼new Promise(function(resolve, reject){let pattern = doParse(file);resolve(pattern);}).then(function(pattern){res.json("pattern1":v1, "pattern2":v2});}).catch(function(exp){res.json(exp);})【在 s*****w 的大作中提到】: my dummy code, try to find values from file, then return it.: try {: parse string from file: if find pattern, throw "found it": } catch (e) {: if(e !== "found it") throw e: } finally {: res.json("pattern1":v1, "pattern2":v2});: }: 问题是,假如文件中没找到,应该如何送一个空的json回去?
s*w2016-05-06 07:0511 楼好像没改啥?【在 b***i 的大作中提到】: a={""};: try {: parse string from file: if find pattern, throw "found it": } catch (e) {: if(e !== "found it") throw e: a={"pattern1":v1, "pattern2":v2};: } finally {: res.json(a);: }
s*w2016-05-06 07:0512 楼嚓,promise in node.js, 头晕死了【在 l**********n 的大作中提到】: new Promise(function(resolve, reject){: let pattern = doParse(file);: resolve(pattern);: }).then(function(pattern){: res.json("pattern1":v1, "pattern2":v2});: }): .catch(function(exp){: res.json(exp);: })