javascript 尝试/抓住oneliner可用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5126560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:03:35  来源:igfitidea点击:

Try/catch oneliner available?

javascripttry-catchshorthand

提问by pimvdb

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.

有没有办法以速记方式做到这一点,最好是oneliner?我当然可以,只是删除了换行,但我宁愿意思是这样的? :事情if

Thanks.

谢谢。

采纳答案by rene

You could use the following function and then use that to oneline your try/catch. It's use would be limited and makes the code harder to maintain so i'll never use it.

您可以使用以下函数,然后使用它来联机您的 try/catch。它的使用会受到限制并使代码更难维护,所以我永远不会使用它。

var v = tc(MyTryFunc, MyCatchFunc);

tc(function() { alert('try'); }, function(e) { alert('catch'); });


/// try/catch 
function tc(tryFunc, catchFunc) {
     var val;
     try {
        val = tryFunc();
     }
     catch (e) {
         val = catchFunc(e);
     }
     return val;
} 

回答by In silico

No, there isn't a "one-liner" version of try-catchbesides simply removing all the newlines.

没有,没有的“一个班轮”的版本try-catch除了简单地删除所有换行符。

Why would you want to? Vertical space doesn't cost you anything.

你为什么要?垂直空间不会花费您任何费用。

And even if you'll settle for removing all the newlines, this, in my opinion, is harder to read:

即使您愿意删除所有换行符,在我看来,这也更难阅读:

try{t = someFunc();}catch(e){t = somethingElse;}

than this:

比这个:

try {
    t = someFunc();
} catch(e) {
    t = somethingElse;
}

What you have is perfectly fine. Readable code should be a priority. Even if it means more typing.

你所拥有的一切都很好。可读代码应该是一个优先事项。即使这意味着更多的打字。

回答by Chris

You can get it down to two lines.

您可以将其缩减为两行。

try { doSomething(); }
catch (e) { handleError(); }

Or, in your specific example, 3 lines.

或者,在您的具体示例中,3 行。

var t;
try { t = doSomething(); }
catch (e) { t = doSomethingElse(); }

Either way, if your code allows for it, a two liner is much more concise, IMO, than the typical try/catch block.

无论哪种方式,如果您的代码允许,那么两行代码比典型的 try/catch 块更简洁,IMO。

回答by coderaiser

There is one liner available as npm package try-catch. You can use it this way:

有一个 liner 可用作 npm 包try-catch。你可以这样使用它:

const tryCatch = require('try-catch');
const {parse} = JSON;

const [error, result] = tryCatch(parse, 'hello');

There is similar approach for async-awaittry-to-catch:

async-awaittry-to-catch也有类似的方法:

const {readFile} = require('fs').promises;

read('./package.json').then(console.log);

async function read(path) {
    const [error, data] = await tryToCatch(readFile, path, 'utf8');

    return data || error.message;
}

All this wrappers do is wrap one function with try-catchblock and uses destructuring to get result.

所有这些包装器所做的就是用try-catch块包装一个函数并使用解构来获得结果。

Also there is an ideato use something similar to Go style error handling:

还有一个想法是使用类似于Go 风格的错误处理

// this is not real syntax
const [error, result] = try parse('hello');