/
except.java
73 lines (63 loc) · 1.72 KB
/
except.java
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
// $Id: except.java,v 1.1 2004-05-23 04:36:29 bfulgham Exp $
// http://www.bagley.org/~doug/shootout/
// Collection class code is from my friend Phil Chu, Thanks Phil!
import java.io.*;
import java.util.*;
import java.text.*;
class Lo_Exception extends Exception {
int num = 0;
public Lo_Exception(int num) {
this.num = num;
}
public String toString() {
return "Lo_Exception, num = " + this.num;
}
}
class Hi_Exception extends Exception {
int num = 0;
public Hi_Exception(int num) {
this.num = num;
}
public String toString() {
return "Hi_Exception, num = " + this.num;
}
}
public class except {
static int Lo = 0;
static int Hi = 0;
public static void main(String args[]) throws IOException {
int n = Integer.parseInt(args[0]);
for (int i=0; i<n; i++) {
some_function(i);
}
System.out.println("Exceptions: HI=" + Hi + " / LO=" + Lo);
}
public static void some_function(int n) {
try {
hi_function(n);
} catch (Exception e) {
System.out.println("We shouldn't get here: " + e);
}
}
public static void hi_function(int n) throws Hi_Exception, Lo_Exception {
try {
lo_function(n);
} catch (Hi_Exception e) {
Hi++;
}
}
public static void lo_function(int n) throws Hi_Exception, Lo_Exception {
try {
blowup(n);
} catch (Lo_Exception e) {
Lo++;
}
}
public static void blowup(int n) throws Hi_Exception, Lo_Exception {
if ((n % 2) == 0) {
throw new Lo_Exception(n);
} else {
throw new Hi_Exception(n);
}
}
}