在Java中,有时我会获得解析用户输入的异常。而不是打印堆栈跟踪,我希望它干净利落地#34;将输出重定向到字符串。示例(使用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
答案 0 :(得分:4)
您可以捕获异常并打印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
}