forked from mthli/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptTest.java
More file actions
176 lines (158 loc) · 4.49 KB
/
ExceptTest.java
File metadata and controls
176 lines (158 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package except;
import java.awt.EventQueue;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import javax.swing.*;
/**
* @version 1.33 2007-06-12
* @author Cay Horstmann
*/
public class ExceptTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new ExceptTestFrame();
frame.setTitle("ExceptTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* A frame with a panel for testing various exceptions
*/
class ExceptTestFrame extends JFrame
{
public ExceptTestFrame()
{
ExceptTestPanel panel = new ExceptTestPanel();
add(panel);
pack();
}
}
/**
* A panel with radio buttons for running code snippets and studying their exception behavior
*/
class ExceptTestPanel extends Box
{
private ButtonGroup group;
private JTextField textField;
private double[] a = new double[10];
public ExceptTestPanel()
{
super(BoxLayout.Y_AXIS);
group = new ButtonGroup();
// add radio buttons for code snippets
addRadioButton("Integer divide by zero", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a[1] = 1 / (a.length - a.length);
}
});
addRadioButton("Floating point divide by zero", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a[1] = a[2] / (a[3] - a[3]);
}
});
addRadioButton("Array bounds", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a[1] = a[10];
}
});
addRadioButton("Bad cast", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a = (double[]) event.getSource();
}
});
addRadioButton("Null pointer", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.out.println(textField.getAction().toString());
}
});
addRadioButton("sqrt(-1)", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a[1] = Math.sqrt(-1);
}
});
addRadioButton("Overflow", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a[1] = 1000 * 1000 * 1000 * 1000;
int n = (int) a[1];
System.out.println(n);
}
});
addRadioButton("No such file", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
System.out.println(new Scanner(Paths.get("woozle.txt")).next());
}
catch (IOException e)
{
textField.setText(e.toString());
}
}
});
addRadioButton("Throw unknown", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
throw new UnknownError();
}
});
// add the text field for exception display
textField = new JTextField(30);
add(textField);
}
/**
* Adds a radio button with a given listener to the panel. Traps any exceptions in the
* actionPerformed method of the listener.
* @param s the label of the radio button
* @param listener the action listener for the radio button
*/
private void addRadioButton(String s, ActionListener listener)
{
JRadioButton button = new JRadioButton(s, false)
{
// the button calls this method to fire an
// action event. We override it to trap exceptions
protected void fireActionPerformed(ActionEvent event)
{
try
{
textField.setText("No exception");
super.fireActionPerformed(event);
}
catch (Exception e)
{
textField.setText(e.toString());
}
}
};
button.addActionListener(listener);
add(button);
group.add(button);
}
}