finallytest.groovy

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 117 行

GROOVY
117
字号
class FinallyTest extends GroovyTestCase{
 
  void testBreakInTry() {
    def called = false
    while (true){
      try {
        break
      } finally {
        called = true
      }
    }
    assert called, "finally block was not called"
  }
  
  void testBreakInFinally() {
    def called = false
    while (true){
      try {
        throw new Exception("foo")
      } catch (e) {
        assert e.message == "foo"
      } finally {
        called = true
        break
      }
    }
    assert called, "finally block was not called"
  }
  
  void testContinueInTry() {
    def called = false
    boolean b = true
    while (b){
      try {
        b=false
        continue
      } finally {
        called = true
      }
    }
    assert called, "finally block was not called"
  }
  
  void testContinueInFinally() {
    def called = false
    boolean b = true
    while (b){
      try {
        throw new Exception("foo")
      } catch (e) {
        assert e.message == "foo"
      } finally {
        b=false
        called = true
        continue
      }
    }
    assert called, "finally block was not called"
  }
  
  void testReturn() {
    def map = methodWithReturnInTry()
    assert map.called, "finally block was not called"
    def called = methodWithReturnInFinally()
    assert called, "finally block was not called"
  }
  
  def methodWithReturnInTry(){
    def map = [:]
    try {
      return map
    } finally {
	  map.called = true
    }
  }
  
  def methodWithReturnInFinally(){
    try {
      return false
    } finally {
	  return true
    }
  }
  
  void testStackeFinally(){
    def calls = methodWithStackedFinally()
    if (calls==12) {
      assert false,"wrong order of finally blocks"
    }
    assert calls==102 
  }

  def methodWithStackedFinally(){
    def calls = 0
    def first = true;
    try {
      try {
        calls = 0
      } finally {
        calls++
        if (first) {
          first = false
        } else {
          calls += 10
        }
      }
    } finally {
      calls++
      if (first) {
        first = false
      } else {
        calls += 100
      }
    }
    return calls
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?