2

我想从一个try块内退出:

function myfunc
{
   try {
      # Some things
      if(condition) { 'I want to go to the end of the function' }
      # Some other things
   }
   catch {
      'Whoop!'
   }

   # Other statements here
   return $whatever
}

我用 a 测试过break,但这不起作用。如果任何调用代码在循环内,它会中断上层循环。

4

3 回答 3

13

一个额外的脚本块try/catchreturn它可能会这样做:

function myfunc($condition)
{
    # Extra script block, use `return` to exit from it
    .{
        try {
            'some things'
            if($condition) { return }
            'some other things'
        }
        catch {
            'Whoop!'
        }
    }
    'End of try/catch'
}

# It gets 'some other things' done
myfunc

# It skips 'some other things'
myfunc $true
于 2012-11-03T17:34:49.270 回答
2

做你想做的事的规范方法是否定条件并将“其他事情”放入“then”块中。

function myfunc {
  try {
    # some things
    if (-not condition) {
      # some other things
    }
  } catch {
    'Whoop!'
  }

  # other statements here
  return $whatever
}
于 2012-11-04T13:31:47.943 回答
1

你可以这样做:

function myfunc
{
   try {
      # Some things
      if(condition)
      {
          goto(catch)
      }
      # Some other things
   }
   catch {
      'Whoop!'
   }

   # Other statements here
   return $whatever
}
于 2014-12-22T13:54:21.803 回答