Reputation: 433
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
Upvotes: 0
Views: 588
Reputation: 788
yes you should put
try { BarcodeNo=Long.parseLong(jTextField1.getText()); } catch(Exception e) { throw new NumberFormatException("Enter Numbers Only "); }
Upvotes: -1
Reputation: 1503230
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.
Upvotes: 2