A simple try block (catches and ignores the error):
try this.doSomething() end
Shortened to:
try : this.doSomething()
Example:
i int
try : i = int.parse("twenty")
Handling the error:
try this.doSomething() catch e Error Console.log(e) end
Shortened to:
try : this.doSomething() catch e Error : Console.log(e)
Multiple types of errors
try
arg = this.doSomethingA()
this.doSomethingB(arg)
catch e InvalidArgumentError
Console.log("Argument was invalid")
catch e Error
Console.log("Something else went wrong")
end
Or shorter
try
arg = this.doSomethingA()
this.doSomethingB(arg)
catch e InvalidArgumentError : Console.log("Argument was invalid")
catch e Error : Console.log("Something else went wrong")
try this.doSomething() catch e Error Console.log(e) finally this.cleanup() end
Shortened to:
try : this.doSomething() catch e Error : Console.log(e) finally : this.cleanup()
throw new NotImplementedError()
Provide an error text
if x > 100 : throw new ArgumentOutOfRangeError("x must be in the range of 0 to 100")
Rethrowing (stacktrace will change):
try this.doSomethingA() this.doSomethingB() catch e Error throw e end
Or shorter:
try this.doSomethingA() this.doSomethingB() catch e Error : throw e
Wrapping an error (cause):
try
this.doSomething()
catch e Error
throw new MyCustomError("Something went wrong", e)
end
For more custom error classes extend from Error base class
package MyLib
using LimaScript
class MyCustomError extends Error
constructor(code int)
Console.log(code)
end
end