WoW:API pcall: Difference between revisions

no edit summary
m (Move page script moved page API pcall to WoW:API pcall without leaving a redirect)
No edit summary
 
Line 18: Line 18:


== Example ==
== Example ==
function myTest(incrementVal)
<pre>
  return incrementVal + 10;
function myTest(incrementVal)
end
  return incrementVal + 10;
end
   
   
local retOK, ret1 = pcall(myTest,"string value");
local retOK, ret1 = pcall(myTest,"string value");
local msg = "";
local msg = "";
if (retOK) then
if retOK then
   msg = "Function succeeded, result: " .. ret1 .. ".";
   msg = "Function succeeded, result: " .. ret1 .. ".";
else
else
   msg = "Function failed, error text: " .. ret1 .. ".";
   msg = "Function failed, error text: " .. ret1 .. ".";
end
end
DEFAULT_CHAT_FRAME:AddMessage(msg);
DEFAULT_CHAT_FRAME:AddMessage(msg);
</pre>


====Result====
==== Result ====
pcall will catch the error that occured within 'myTest', and output the appropriate text to the default chat window. If "string value" is replaced by a number, the code will output number+10 instead.
'pcall' will catch the error that occurred within 'myTest', and output the appropriate text to the default chat window. If "string value" is replaced by a number, the code will output number+10 instead.


==Details==
==Details==
: pcall will ''also'' return other arguments returned from the "function" if all is successful.
: pcall will ''also'' return other arguments returned from the "function" if all is successful.