2010-12-29 00:48:54 -05:00
|
|
|
# Exception Handling
|
|
|
|
# ------------------
|
|
|
|
|
|
|
|
# shared nonce
|
|
|
|
nonce = {}
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Throw
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "basic exception throwing", ->
|
|
|
|
throws (-> throw 'error'), 'error'
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Empty Try/Catch/Finally
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "try can exist alone", ->
|
|
|
|
try
|
|
|
|
|
|
|
|
test "try/catch with empty try, empty catch", ->
|
|
|
|
try
|
|
|
|
# nothing
|
|
|
|
catch err
|
|
|
|
# nothing
|
|
|
|
|
|
|
|
test "single-line try/catch with empty try, empty catch", ->
|
|
|
|
try catch err
|
|
|
|
|
|
|
|
test "try/finally with empty try, empty finally", ->
|
|
|
|
try
|
|
|
|
# nothing
|
|
|
|
finally
|
|
|
|
# nothing
|
|
|
|
|
|
|
|
test "single-line try/finally with empty try, empty finally", ->
|
|
|
|
try finally
|
|
|
|
|
|
|
|
test "try/catch/finally with empty try, empty catch, empty finally", ->
|
|
|
|
try
|
|
|
|
catch err
|
|
|
|
finally
|
|
|
|
|
|
|
|
test "single-line try/catch/finally with empty try, empty catch, empty finally", ->
|
|
|
|
try catch err then finally
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Try/Catch/Finally as an Expression
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "return the result of try when no exception is thrown", ->
|
|
|
|
result = try
|
|
|
|
nonce
|
|
|
|
catch err
|
|
|
|
undefined
|
|
|
|
finally
|
|
|
|
undefined
|
|
|
|
eq nonce, result
|
|
|
|
|
|
|
|
test "single-line result of try when no exception is thrown", ->
|
|
|
|
result = try nonce catch err then undefined
|
|
|
|
eq nonce, result
|
|
|
|
|
|
|
|
test "return the result of catch when an exception is thrown", ->
|
|
|
|
fn = ->
|
|
|
|
try
|
|
|
|
throw ->
|
|
|
|
catch err
|
|
|
|
nonce
|
|
|
|
doesNotThrow fn
|
|
|
|
eq nonce, fn()
|
|
|
|
|
|
|
|
test "single-line result of catch when an exception is thrown", ->
|
|
|
|
fn = ->
|
|
|
|
try throw (->) catch err then nonce
|
|
|
|
doesNotThrow fn
|
|
|
|
eq nonce, fn()
|
|
|
|
|
|
|
|
test "optional catch", ->
|
|
|
|
fn = ->
|
|
|
|
try throw ->
|
|
|
|
nonce
|
|
|
|
doesNotThrow fn
|
|
|
|
eq nonce, fn()
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Try/Catch/Finally Interaction With Other Constructs
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "try/catch with empty catch as last statement in a function body", ->
|
|
|
|
fn = ->
|
|
|
|
try nonce
|
|
|
|
catch err
|
|
|
|
eq nonce, fn()
|
2011-08-14 17:08:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Catch leads to broken scoping: #1595
|
|
|
|
|
|
|
|
test "try/catch with a reused variable name.", ->
|
|
|
|
do ->
|
|
|
|
try
|
|
|
|
inner = 5
|
|
|
|
catch inner
|
|
|
|
# nothing
|
|
|
|
eq typeof inner, 'undefined'
|
|
|
|
|