如何捕捉Java中的异常?我有一个程序接受用户输入的整数值。现在如果用户输入一个无效值,它会抛出一个java.lang.NumberFormatException
。我如何捕获该异常?如何在Java中捕捉异常?
public void actionPerformed(ActionEvent e) {
String str;
int no;
//------------------------------------
try {
//lots of ifs here
} catch (NumberFormatException e) {
//do something with the exception you caught
}
if (e.getSource() == finish) {
if (message.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Please Enter the Input First");
} else {
leftButtons();
}
}
//rest of your code
}
您需要读取整个堆栈跟踪。这个异常是否在你的代码中抛出? –
http://docs.oracle.com/javase/tutorial/essential/exceptions/和http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html和http://docs.oracle。 com/javase/tutorial/essential/exceptions/catch.html –
请点击这里http://docs.oracle.com/javase/tutorial/essential/exceptions/ – titogeo