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 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.

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;
} 



回答2:


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

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.




回答3:


You can get it down to two lines.

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

Or, in your specific example, 3 lines.

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.




回答4:


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

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

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

There is similar approach for async-await try-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-catch block and uses destructuring to get result.

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

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


来源:https://stackoverflow.com/questions/5126560/try-catch-oneliner-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!