日期:2014-05-20  浏览次数:20830 次

JAVA 异常处理问题
Java code
try {
                     
                    int s = Integer.parseInt(t2.getText());
                } catch (NumberFormatException h) {
                    JOptionPane.showMessageDialog(null,
                            "Array can only contain integer number",
                            "Invaild number", JOptionPane.ERROR_MESSAGE);
                }




这个的异常不捕获怎么办?
只是停止工作。。。求高手解答。。。

------解决方案--------------------
可以的啊
------解决方案--------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Exception extends JFrame implements ActionListener{
JTextField t2;
JButton jb;
public Exception(){
JPanel jp = new JPanel();
JLabel jl = new JLabel("hello");
t2 = new JTextField(10);
jb = new JButton("输出");
jp.add(jb);
jp.add(jl);
jp.add(t2);
add(jp);
jb.addActionListener(this);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Exception e = new Exception();
e.setSize(500,600);
e.setDefaultCloseOperation(EXIT_ON_CLOSE);
e.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {

int s = Integer.parseInt(t2.getText());
System.out.println(s);
} catch (NumberFormatException h) {
JOptionPane.showMessageDialog(null,
"Array can only contain integer number", "Invaild number",
JOptionPane.ERROR_MESSAGE);
}
}

}
可以的 我试过了 上面是代码
------解决方案--------------------
我也遇到类似的问题了,现在还没有找到办法
------解决方案--------------------
不捕获这个异常,如果发生了,就停止程序,在控制台打印堆栈信息