2 final static int COLUMN = 55;
4 final static int INDEX1 = 0xcafebabe;
5 final static int INDEX2 = 0xbabecafe;
6 final static int INDEX3 = 0xdeadbeef;
8 static boolean printStackTrace;
10 public static void main(String[] argv) {
11 printStackTrace = true;
13 Runtime r = Runtime.getRuntime();
14 int maxmem = (int) r.maxMemory();
16 // if (argv.length > 0)
17 // if (argv[0].equals("stacktrace"))
18 // printStackTrace = true;
20 boolean caught = false;
22 pheader("normal exceptions");
25 p("throw new Exception():");
26 throw new Exception();
27 } catch (Exception e) {
32 /* check if catch block was executed */
39 p("throw new Exception() (from subroutines):");
42 } catch (Exception e) {
48 p("NullPointerException:");
52 } catch (NullPointerException e) {
60 pheader("exceptions thrown in JIT code");
63 p("ArithmeticException (only w/ -softnull):");
64 int i = 1, j = 0, k = i / j;
66 } catch (ArithmeticException e) {
67 String msg = e.getMessage();
69 if (msg == null || !msg.equals("/ by zero")) {
70 pln("FAILED: wrong message: " + msg + ", should be: / by zero");
79 p("ArrayIndexOutOfBoundsException:");
80 int[] ia = new int[1];
83 } catch (ArrayIndexOutOfBoundsException e) {
84 String msg = e.getMessage();
86 if (msg == null || !msg.equals(String.valueOf(INDEX1))) {
87 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX1);
96 p("ArrayStoreException:");
97 Integer[] ia = new Integer[1];
98 Object[] oa = (Object[]) ia;
101 } catch (ArrayStoreException e) {
107 p("ClassCastException:");
108 Object o = new Object();
109 Integer i = (Integer) o;
111 } catch (ClassCastException e) {
117 p("NegativeArraySizeException (newarray):");
118 int[] ia = new int[-1];
120 } catch (NegativeArraySizeException e) {
126 p("NegativeArraySizeException (multianewarray):");
127 int[][] ia = new int[1][-1];
129 } catch (NegativeArraySizeException e) {
135 p("OutOfMemoryError:");
136 /* maxmem + 1 should be enough and hopefully not overflow the int so it becomes negative */
137 byte[] ba = new byte[maxmem];
139 } catch (OutOfMemoryError e) {
145 p("OutOfMemoryError (multianewarray):");
146 byte[][] ba = new byte[maxmem][maxmem];
148 } catch (OutOfMemoryError e) {
156 pheader("exceptions in leaf functions");
159 p("ArithmeticException:");
162 } catch (ArithmeticException e) {
168 p("ArrayIndexOutOfBoundsException:");
169 aioobesub(new int[1]);
171 } catch (ArrayIndexOutOfBoundsException e) {
172 String msg = e.getMessage();
174 if (msg == null || !msg.equals(String.valueOf(INDEX3))) {
175 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX3);
185 p("ClassCastException:");
186 ccesub(new Object(), new Integer(0));
188 } catch (ClassCastException e) {
194 p("NullPointerException:");
197 } catch (NullPointerException e) {
203 p("Exception in <clinit> triggered from a leaf method:");
204 extest_clinit_patcher.i = 1;
206 } catch (ExceptionInInitializerError e) {
214 pheader("exception related things");
217 p("load/link an exception class in asmpart:");
218 throw new Exception();
219 } catch (UnknownError e) {
220 /* this exception class MUST NOT be loaded before!!!
221 otherwise this test is useless */
222 } catch (Exception e) {
230 pheader("native stub exceptions");
233 p("NullPointerException in <clinit>:");
236 } catch (ExceptionInInitializerError e) {
242 p("UnsatisfiedLinkError:");
245 } catch (UnsatisfiedLinkError e) {
251 p("NullPointerException (native):");
252 System.arraycopy(null, 1, null, 1, 1);
254 } catch (NullPointerException e) {
262 pheader("special exceptions");
265 p("OutOfMemoryError (array clone):");
266 /* use half of the heap size */
267 byte[] ba1 = new byte[maxmem / 2];
268 byte[] ba2 = (byte[]) ba1.clone();
270 } catch (OutOfMemoryError e) {
278 pheader("exception thrown to command-line");
280 pln("NullPointerException (without catch):");
286 synchronized static void sub() throws Exception {
290 static void sub2() throws Exception {
294 synchronized static void sub3() throws Exception {
298 static void sub4() throws Exception {
299 throw new Exception();
302 static void aesub(int a, int b) {
306 static void aioobesub(int[] ia) {
310 static void ccesub(Object o, Integer i) {
314 static void npesub(int[] ia) {
318 static native void nsub();
320 static void p(String s) {
322 for (int i = s.length(); i < COLUMN; i++) {
323 System.out.print(" ");
327 static void pheader(String s) {
329 for (int i = s.length(); i < COLUMN + 3; i++) {
330 System.out.print("-");
332 System.out.println();
333 System.out.println();
337 System.out.println();
340 static void pln(String s) {
341 System.out.println(s);
348 static void failed() {
352 static void pstacktrace(Throwable e) {
353 if (!printStackTrace)
356 System.out.println();
360 class extest_clinit {
366 public static native void sub();
369 class extest_clinit_patcher {