Java GUI โปรแกรมบวกเลข ที่ดัก Exception แล้ว
ตัวอย่างโค้ดในบทความ เรายังไม่ได้ดัก Exception ซึ่งจะทำให้เกิด Exception
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "asdf"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at com.doesystem.JavaCodeExam.actionPerformed(JavaCodeExam.java:53)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
ดังนั้น ในตัวอย่างโค้ดนี้เรามาดูตัวอย่างโค้ดที่ได้ทำการดัก Exception กันคับ
ตัวอย่างโค้ด
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.JPanel;
import javax.swing.JTextField;
public class JavaCodeExam extends JFrame implements ActionListener {
private JFrame frame;
private JTextField txtField1;
private JTextField txtField2;
private JLabel lblAns;
public static void main(String[] args) throws Exception {
new JavaCodeExam();
}
public JavaCodeExam() {
this.frame = new JFrame();
this.frame.setVisible(true);
this.frame.setTitle("Frame By Java Code Exam");
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.txtField1 = new JTextField(5);
this.txtField2 = new JTextField(5);
panel.add(this.txtField1);
panel.add(new JLabel(" + "));
panel.add(this.txtField2);
JButton btnAlert = new JButton("=");
btnAlert.addActionListener(this);
panel.add(btnAlert);
this.lblAns = new JLabel();
panel.add(this.lblAns);
this.frame.add(panel);
this.frame.pack();
this.frame.setSize(500, 200);
this.frame.setLocation(10, 10);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
double val1 = Double.parseDouble(this.txtField1.getText());
double val2 = Double.parseDouble(this.txtField2.getText());
this.lblAns.setText((val1 + val2) + "");
} catch (NumberFormatException ex) {
ex.printStackTrace();
this.lblAns.setText("Cannot a Number");
} catch (Exception ex) {
ex.printStackTrace();
this.lblAns.setText(ex.getMessage());
}
}
}
ตัวอย่างนี้เราได้ทำการเพิ่ม try catch ให้กับโค้ด และดักจับ Exception โดยถ้าเกิด NumberFormatException ให้แสดงข้อความว่า Cannot a Number แต่ถ้าเกิด Exception อื่น ๆ จะแสดง message exception นั้น ๆ
ใน catch เราได้ใส่ Exception ไว้ในตัวแปร ex และได้ทำการ printStackTrace เพื่อแสดงค่า Exceeption ใน Console
ไม่มีความคิดเห็น :
แสดงความคิดเห็น