forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.java
68 lines (64 loc) · 1.44 KB
/
src.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
/**
* 99 Bottles in Java - Exception Oriented Programming :-)
* This program abuses concept of exceptions.
* notice: it does not use any loops (for, while) or if statements
*
* use java 1.4 or newer
* @author Jarek Ratajski
*/
public class EOP99Bottles
{
/**
* this main method is very simple -
* only throws exception and prints stack trace
* would You suspect it is 99 bottles program ? :-)
*/
public static void main(String[] args)
{
try
{
throw new BottleException(1, null);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static class BottleException extends Exception
{
private final int cnt;
/**
* notice beauty of this signature: object that may throw exception of
* its own type during its creation !!!
*/
public BottleException(int i, BottleException c)
throws BottleException
{
super(c);
this.cnt = i;
try
{
int a = 03 / (99-i);
throw new BottleException(i+1, this);
}
catch (ArithmeticException e)
{
//deliberately
}
}
public void printStackTrace()
{
System.out.println(cnt+" Bottle(s) of beer on the wall,"+cnt+"bottle(s) of beer");
System.out.println("Take one down and pass it around,");
System.out.println((cnt-1)+" bottle(s) of beer on the wall");
try
{
getCause().printStackTrace();
}
catch (NullPointerException npe)
{
//deliberately
}
}
}
}