这是我用 transcrypt -b -n -m (version 3.6.84) 转码的 python 代码:
def test_1():
console.log('before assert')
assert False, "False fails"
console.log('we should not see that if assert fails as expected')
def test_2():
console.log('before assert')
try:
assert False, "False fails"
except AssertionError as exception:
console.log('we should see that since we catch the assertion error')
console.log('after assert')
当我在浏览器控制台中运行 test_1/test_2 时,我得到了一个奇怪的行为:
> mymodule.test_1()
before assert
we should not see that if assert fails as expected
<- undefined
> mymodule.test_2()
before assert
after assert
<- undefined
为什么 assert 不会引发异常?