lua的异常处理

异常处理

function catch(what) 
   return what[1] 
end 

function try(what) 
   status, result = pcall(what[1]) 
   if not status then 
      what[2](result) 
   end 
   return result 
end 



try { 
   function() 
      error('oops') 
   end, 

   catch { 
      function(error) 
         print('caught error: ' .. error) 
      end 
   } 
 } 

所以可以用下面的办法

local status, result = pcall( function() 
trace("1111") 
end 
) 
if not status then  
print(result) 
end  

Leave a Reply

Your email address will not be published. Required fields are marked *