嗨,我有下面的伪代码,抛出这样的异常
throw new MyException("Bad thing happened","com.stuff.errorCode");
其中 MyException 扩展了 Exception 类。下面是我处理异常的方式
ActionMessages errors = new ActionMessages();
if(ex.getErrorCode() != null && !"".equals(ex.getErrorCode()))
error = new ActionMessage(ex.getErrorCode()); // com.stuff.errorCode is the errorCode
else
error = new ActionMessage(ex.getMessage()); //"Bad thing happened" is error message
errors.add(ActionMessages.GLOBAL_MESSAGE, error);
//errors.add(Globals.MESSAGE_KEY, error );
saveErrors( requ, errors );
return (mapping.findForward(nextScreen));
现在,在用户屏幕上,消息显示为
???en_US.Bad thing happened???
我正在使用 Struts 1.2.4,我正在研究它的源代码,但任何指针都将不胜感激。
最佳答案
编辑:这个答案是在问题发生实质性改变之前写的。
“???en_US”必须来自其他地方(可能是您用于打印消息的 Web 框架),因为 Exception
本身不执行任何查找。
编译此代码:
public class MyException extends Exception {
public MyException(String msg, String sErrorCode) {
super(msg);
// ignore sErrorCode
}
}
public class MyExceptionTest {
public static void main(String[] args) {
System.out.println(new MyException("foo", "bar").getMessage());
}
}
并运行MyExceptionTest
,您将看到一个简单的“foo”作为输出(正如预期的那样)。
关于java - Throwable getMessage() 的奇怪输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2432587/