2 final static int INDEX1 = 0xcafebabe;
3 final static int INDEX2 = 0xbabecafe;
4 final static int INDEX3 = 0xdeadbeef;
6 static boolean printStackTrace;
8 public static void main(String[] argv) {
9 printStackTrace = true;
11 Runtime r = Runtime.getRuntime();
12 int maxmem = (int) r.maxMemory();
14 // if (argv.length > 0)
15 // if (argv[0].equals("stacktrace"))
16 // printStackTrace = true;
18 boolean catched = false;
20 pln("---------- normal exceptions --------------------");
23 p("throw new Exception():");
24 throw new Exception();
25 } catch (Exception e) {
30 /* check if catch block was executed */
37 p("throw new Exception() (from subroutines):");
40 } catch (Exception e) {
46 p("NullPointerException:");
50 } catch (NullPointerException e) {
58 pln("---------- test soft inline exceptions ----------");
59 pln("/* thrown twice to check the inline jump code */");
62 p("ArithmeticException (only w/ -softnull):");
63 int i = 1, j = 0, k = i / j;
65 } catch (ArithmeticException e) {
66 String msg = e.getMessage();
68 if (msg == null || !msg.equals("/ by zero")) {
69 pln("FAILED: wrong message: " + msg + ", should be: / by zero");
78 p("ArithmeticException (only w/ -softnull):");
79 long i = 1, j = 0, k = i / j;
81 } catch (ArithmeticException e) {
82 String msg = e.getMessage();
84 if (msg == null || !msg.equals("/ by zero")) {
85 pln("FAILED: wrong message: " + msg + ", should be: / by zero");
95 p("ArrayIndexOutOfBoundsException:");
96 int[] ia = new int[1];
99 } catch (ArrayIndexOutOfBoundsException e) {
100 String msg = e.getMessage();
102 if (msg == null || !msg.equals(String.valueOf(INDEX1))) {
103 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX1);
112 p("ArrayIndexOutOfBoundsException:");
113 int[] ia = new int[1];
116 } catch (ArrayIndexOutOfBoundsException e) {
117 String msg = e.getMessage();
119 if (msg == null || !msg.equals(String.valueOf(INDEX2))) {
120 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX2);
130 p("ArrayStoreException:");
131 Integer[] ia = new Integer[1];
132 Object[] oa = (Object[]) ia;
133 oa[0] = new Object();
135 } catch (ArrayStoreException e) {
141 p("ArrayStoreException:");
142 Integer[] ia = new Integer[1];
143 Object[] oa = (Object[]) ia;
144 oa[0] = new Object();
146 } catch (ArrayStoreException e) {
153 p("ClassCastException:");
154 Object o = new Object();
155 Integer i = (Integer) o;
157 } catch (ClassCastException e) {
163 p("ClassCastException:");
164 Object o = new Object();
168 } catch (ClassCastException e) {
175 p("NegativeArraySizeException (newarray):");
176 int[] ia = new int[-1];
178 } catch (NegativeArraySizeException e) {
184 p("NegativeArraySizeException (newarray):");
185 int[] ia = new int[-1];
187 } catch (NegativeArraySizeException e) {
193 p("NegativeArraySizeException (multianewarray):");
194 int[][] ia = new int[1][-1];
196 } catch (NegativeArraySizeException e) {
203 p("NullPointerException (only w/ -softnull):");
207 } catch (NullPointerException e) {
213 p("NullPointerException (only w/ -softnull):");
217 } catch (NullPointerException e) {
223 p("OutOfMemoryError:");
224 /* use twice the heap size */
225 byte[] ba = new byte[maxmem * 2];
227 } catch (OutOfMemoryError e) {
233 p("OutOfMemoryError:");
234 /* use twice the heap size */
235 byte[] ba = new byte[maxmem * 2];
237 } catch (OutOfMemoryError e) {
243 p("OutOfMemoryError (multianewarray):");
244 byte[][] ba = new byte[maxmem][maxmem];
246 } catch (OutOfMemoryError e) {
254 pln("---------- exceptions in leaf functions ---------");
257 p("ArithmeticException:");
260 } catch (ArithmeticException e) {
266 p("ArrayIndexOutOfBoundsException:");
267 aioobesub(new int[1]);
269 } catch (ArrayIndexOutOfBoundsException e) {
270 String msg = e.getMessage();
272 if (msg == null || !msg.equals(String.valueOf(INDEX3))) {
273 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX3);
283 p("ClassCastException:");
284 ccesub(new Object(), new Integer(0));
286 } catch (ClassCastException e) {
292 p("NullPointerException:");
295 } catch (NullPointerException e) {
303 pln("---------- exception related things -------------");
306 p("load/link an exception class in asmpart:");
307 throw new Exception();
308 } catch (UnknownError e) {
309 /* this exception class MUST NOT be loaded before!!!
310 otherwise this test is useless */
311 } catch (Exception e) {
319 pln("---------- native stub exceptions ---------------");
322 p("NullPointerException in <clinit>:");
325 } catch (ExceptionInInitializerError e) {
328 } catch (UnsatisfiedLinkError e) {
329 /* catch this one for ENABLE_STATICVM and say it's ok */
335 p("UnsatisfiedLinkError:");
338 } catch (UnsatisfiedLinkError e) {
344 p("NullPointerException (native):");
345 System.arraycopy(null, 1, null, 1, 1);
347 } catch (NullPointerException e) {
355 pln("---------- special exceptions -------------------");
358 p("OutOfMemoryError (array clone):");
359 /* use half of the heap size */
360 byte[] ba1 = new byte[maxmem / 2];
361 byte[] ba2 = (byte[]) ba1.clone();
363 } catch (OutOfMemoryError e) {
371 pln("---------- no OK beyond this point --------------");
373 pln("NullPointerException (without catch):");
379 synchronized static void sub() throws Exception {
383 static void sub2() throws Exception {
387 synchronized static void sub3() throws Exception {
391 static void sub4() throws Exception {
392 throw new Exception();
395 static void aesub(int a, int b) {
399 static void aioobesub(int[] ia) {
403 static void ccesub(Object o, Integer i) {
407 static void npesub(int[] ia) {
411 static native void nsub();
413 static void p(String s) {
415 for (int i = s.length(); i < 46; i++) {
416 System.out.print(" ");
421 System.out.println();
424 static void pln(String s) {
425 System.out.println(s);
432 static void failed() {
436 static void pstacktrace(Throwable e) {
437 if (!printStackTrace)
440 System.out.println();