c#调用dll时尝试
As browser implement new APIs, the truth is that though the APIs provide more power, I'd argue they bring about more volatility. Whether it's the API that's the issue or us trying to use it, you're bound to run into errors which may break parts of your app. Crap. And a try/catch blocks everywhere? Bleh. That's why I use an attempt function in such cases: it keeps the code cleaner and with less side effects.
当浏览器实现新的API时,事实是尽管这些API提供了更多功能,但我认为它们带来了更多的可变性。 无论是问题的API还是我们试图使用它,您都一定会遇到错误,可能会破坏应用程序的某些部分。 废话 哪里有尝试/捕获块? eh 这就是为什么我在这种情况下使用attempt函数的原因:它使代码更整洁且副作用更少。
What we'll do is essentially call the function for the user, catching any crap that comes along:
我们要做的实际上是为用户调用该函数,捕获随之而来的所有废话:
function attempt(fn, args, binding) { try { return fn.apply(binding, args); } catch(e) { console.log('Exception, fix me please', e); } } // Use it! attempt(function() { /* volatile stuff */ }, ['argOne', someVar], this);Provide the function, args, and binding and you're all set. You can use anonymous functions, named functions, whatever. And you don't need to add your own try/catch blocks everywhere. Nothing groundbreaking in the code above but it's safe and easy!
提供函数,参数和绑定,一切就绪。 您可以使用匿名函数,命名函数等。 而且您无需在任何地方添加自己的try / catch块。 上面的代码没有什么突破性的内容,但是它既安全又容易!
翻译自: https://davidwalsh.name/function-attempt
c#调用dll时尝试