[英]How to throw exception for statment?
HI ! 嗨! I want to throw exception for the line
我想抛出异常
BarcodeNo=Long.parseLong(jTextField1.getText())
I done this in a way 我以某种方式做到了
BarcodeNo=Long.parseLong(jTextField1.getText()) throw new NumberFormatException("Enter Numbers Only ");
But this way compiler throws error stating ";" 但是这样,编译器会抛出错误,指出“;” required
需要
So anyone can tell me how to do this ? 那么谁能告诉我该怎么做?
Thanks 谢谢
That will already thrown an exception if the text isn't in the right format. 如果文本格式不正确,那将已经引发异常。 If you want to change the exception message , you'd have to catch the exception and throw a new one:
如果要更改异常消息 ,则必须捕获异常并引发新的异常:
try {
BarcodeNo = Long.parseLong(jTextField1.getText());
} catch (NumberFormatException e) {
throw new NumberFormatException("Enter Numbers Only");
}
I wouldn't suggest that you try to use exception message as user-visible messages though - they're more reasonable for logging than for showing an end user. 我不建议您尝试将异常消息用作用户可见的消息-与记录最终用户相比,日志记录更合理。
yes you should put 是的,你应该把
try { BarcodeNo=Long.parseLong(jTextField1.getText()); } catch(Exception e) { throw new NumberFormatException("Enter Numbers Only "); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.