2

I am using luajava. When lua execute wrong,I cannot catch exception,and then jdk crashed. So how can I catch exception in lua?I just catch error like this(java code):

LuaState ls = LuaStateFactory.newLuaState();
ls.openLibs();
String luaPath = "test.lua";
int isCompile = ls.LdoFile(luaPath);
if(isCompile==0){
    log.info(luaPath+" compile success!");
}else{
    log.info(luaPath+" script does not exist or compile failed!");
}

When lua has internal error,I cannot catch. So how can I catch exception in lua?

When lua executes error, JVM shows an error, not an exception. How can I catch the error in Java?

gaochao
  • 21
  • 2
  • Do not know anything about luaJava. But in lua you could wrap the failing function in a [`pcall`-call](http://www.lua.org/pil/8.4.html). Couldn't you do the same in luajava with its [`call` function](http://www.keplerproject.org/luajava/API/index.html). – Steven Jun 22 '11 at 10:36

2 Answers2

2

Bit of a hack, but the only way I can think of to fix this is to do something like this:

LuaState ls = LuaStateFactory.newLuaState();
ls.openLibs();
String luaPath = "test.lua";
int isCompile;
try {
    isCompile = ls.LdoFile(luaPath);
} catch (Exception ex {
    ex.printStackTrace(System.err);
    isCompile = 1;
}
if(isCompile==0){
    log.info(luaPath+" compile success!");
}else{
    log.info(luaPath+" script does not exist or compile failed!");
}

Sorry if this isn't what your asking, but the LuaJava doc is worthless, so I have no idea what the specific runtime exception is.

bob_twinkles
  • 252
  • 2
  • 9
  • It isn't the point.Thank you all the same. When lua executes error,JVM gives an error,not a exception. – gaochao Jun 22 '11 at 09:02
0

LuaState.LdoFile doesn't throw any exceptions. One approach you might try is to spawn a new thread to attempt running the lua script.

MrDrews
  • 2,139
  • 2
  • 22
  • 22