HyperMath

pcall

pcall

Previous topic Next topic No expanding text in this topic  

pcall

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

Executes a function in a protected mode.

Syntax

bStatus, result = pcall(f, arg1, ....)

Arguments

Name

Description

 

f

A function.

 

arg

A variable passed as an argument to the function 'f', equivalent to f(arg1).

Outputs

Name

Description

 

bStatus

A Boolean (true if the call succeeds, false if there is an error).

 

result

The result of the call.  If bStatus is false, a string with the error message.

Example

Syntax

 

function foo(a,b)

  return a + b

end

 

// executes foo function with 3 and 4 as input argument

s1, r1 = pcall(foo, 3, 4)

print(s1);

print(r1);

// executes foo function with 'a' and [3 4] as input argument

// foo generates an error in this case catched by pcall

s2, r2 = pcall(foo, 'a', [3 4])

print(s2);

print(r2);

// example to execute a string if it is possible

b1, m1 = pcall(Eval,'aa = 3')

print(b1);

print(exist('aa'))

print(m1);

// zi does not exist. an error will be created and catched by pcall

b2, m2 = pcall(Eval,'aaa = zi + 3')

print(b2);

print(exist('aaa'))

print(m2);

Comments

The 'pcall' function can be used with 'Eval' to execute a string and detect an error.

See Also:

Eval