我遇到PowerShell问题,即使在catch命令中明确提到异常,它也不会捕获异常.
在这种情况下,我正在尝试确定ProcessID是否仍在运行,如果没有,那么它将采取一些操作.
我正在努力的示例代码块是:
try {
Get-Process -Id 123123 -ErrorAction 'Stop'
}
catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
"Caught by Exception Type: Process is missing"
}
catch {
if ($_.Exception.getType().FullName -eq "Microsoft.PowerShell.Commands.ProcessCommandException") {
"Caught by Catch All: Process is missing"
}
}
Run Code Online (Sandbox Code Playgroud)
执行此代码块时,输出为:
Caught by Catch All: Process is missing
Run Code Online (Sandbox Code Playgroud)
您可能希望触发第一个catch条件,因为它命名正确抛出的异常,但它不会触发.
更糟糕的是,当第二个catch命令运行(捕获任何东西)时,它会查询异常类型的名称并检查它是否是"Microsoft.PowerShell.Commands.ProcessCommandException"(它是),然后采取适当的步骤.
我知道我可以解决这个问题,但我觉得我缺少关于PowerShell如何处理异常的基本方法.
任何人都可以为我阐明这一点吗?
我在使用ArangoJS库发送参数时遇到问题,并且想知道是否有人可以提供帮助.
通过下面的示例,如果参数值在查询中,则可以执行db.query,但是一旦我尝试使用bindVars,我就会收到无提示错误,并且无法提取任何错误详细信息.
var db = require('arangojs')("http://127.0.0.1:8529");
/*
The '_system' database contains a collection called 'test' that contains one document:
{
"a": 1,
"b": 2
}
*/
// This works
db.query('FOR t IN test FILTER t.a == 1 RETURN t')
.then((cursor) => {
cursor.all()
.then(vals => {
console.log("\nNo bindVars");
console.log(vals);
});
});
// This does not work
db.query("FOR t IN @first FILTER t.a == @second RETURN t", { first: "test", second: 1 })
.then((cursor) => {
cursor.all()
.then(vals => {
console.log("\nUsing …Run Code Online (Sandbox Code Playgroud) 我想在PowerShell(v2)脚本上设置一个时间限制,以便在该时间限制到期后强行退出.
我在PHP中看到他们有像set_time_limit和max_execution_time这样的命令,你可以限制脚本甚至函数执行的时间.
使用我的脚本,查看时间的do/while循环是不合适的,因为我正在调用一个可以挂起很长时间的外部代码库.
我想限制一段代码并且只允许它运行x秒,之后我将终止该代码块并向用户返回脚本超时的响应.
我查看了后台作业,但它们在不同的线程中运行,因此不会对父线程具有终止权限.
有没有人处理过此问题或有解决方案?
谢谢!
我已经做了几个月的arangojs,并且最近我的一些代码开始失败,过去工作,关于在使用模板字符串和aqlQuery时使用变量作为集合名称.
我在Windows 10上使用Node.js 4.4.0.
此示例代码显示错误:
// Issue with template string and arangodb with collection names
var arango = require('arangojs');
var aqlQuery = require('arangojs').aqlQuery;
var db = new arango('http://127.0.0.1:8529');
var collectionName = 'sampleCollection';
// Connect to _system
db.useDatabase('_system');
var sampleText = "Testing Only";
/* Add a record to the collection, using template string with collectionName
* If I use ${collectionName} I get the error code 400:
* error: 'bind parameter \'bind parameter \'value1\' has an invalid value or type near
* …Run Code Online (Sandbox Code Playgroud)