在Java中,有时我会得到一个异常解析用户输入。与其打印堆栈跟踪,我希望它“干净”地将输出重定向到字符串。示例(使用LuaJ):
例外:
Exception in thread "Thread-3" org.luaj.vm2.LuaError: [string "jhkfchyufjhdnxvk,gh'bnmcvk,fvjmfk,xdcfsdkckucfdxfds,ie(); "]:1: unfinished string
    at org.luaj.vm2.compiler.LexState.lexerror(Unknown Source)
    at org.luaj.vm2.compiler.LexState.read_string(Unknown Source)
    at org.luaj.vm2.compiler.LexState.llex(Unknown Source)
    at org.luaj.vm2.compiler.LexState.next(Unknown Source)
    at org.luaj.vm2.compiler.LexState.str_checkname(Unknown Source)
    at org.luaj.vm2.compiler.LexState.singlevar(Unknown Source)
    at org.luaj.vm2.compiler.LexState.primaryexp(Unknown Source)
    at org.luaj.vm2.compiler.LexState.suffixedexp(Unknown Source)
    at org.luaj.vm2.compiler.LexState.assignment(Unknown Source)
    at org.luaj.vm2.compiler.LexState.exprstat(Unknown Source)
    at org.luaj.vm2.compiler.LexState.statement(Unknown Source)
    at org.luaj.vm2.compiler.LexState.statlist(Unknown Source)
    at org.luaj.vm2.compiler.LexState.mainfunc(Unknown Source)
    at org.luaj.vm2.compiler.LuaC.luaY_parser(Unknown Source)
    at org.luaj.vm2.compiler.LuaC.compile(Unknown Source)
    at org.luaj.vm2.Globals.compilePrototype(Unknown Source)
    at org.luaj.vm2.Globals.loadPrototype(Unknown Source)
    at org.luaj.vm2.Globals.load(Unknown Source)
    at org.luaj.vm2.Globals.load(Unknown Source)
    at org.luaj.vm2.Globals.load(Unknown Source)
    at net.snugglesstuff.LeoBot.LuaThread.run(LuaThread.java:25)产出应该是:
[string "jhkfchyufjhdnxvk,gh'bnmcvk,fvjmfk,xdcfsdkckucfdxfds,ie(); "]:1: unfinished string发布于 2014-07-24 18:46:21
您可以捕获异常并打印exception.getMessage()。
try {
  ... parsing code ...
}
catch (Exception exc) { // change this to catch the specific type of exceptions relevant to the parsing
    System.out.println(exc.getMessage()); // or redirect the message to a file
}https://stackoverflow.com/questions/24941606
复制相似问题