Post date: Oct 24, 2017 6:17:06 AM
CS F213- Object Oriented Programming
Exceptions (Examples)
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
$vi SumToN_WithoutExecption.java
class SumToN {
private int num, sum;
public SumToN(int aNum) {
setNum(aNum); //how does the err cond returned from constructor
}
public void setNum(int aNum) {
if (aNum < 0) {
System.out.println(aNum + " < 0");
return;
}
num = aNum;
}
public int calcSum() {
sum = 0;
for (int i = 0; i <= num; i++) {sum += i;}
return sum;
}
public void display() {
System.out.println("Sum of " + num + " nos = " + sum);
}
}
public class SumToN_WithoutException {
public static void main(String[] args) {
int n = -30;
SumToN a = new SumToN(n);
//extra checks to check if the n is –ve etc, all programers
// resposibilty..(program gets lot of if/else to handle errors
a.calcSum();
a.display();
}
}
$javac SumToN_WithoutExecption.java
$java SumToN_WithoutExecption
-30 < 0
Sum of 0 nos = 0 (This can be avoided with extra checks)
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
$vi SumToN_WithException.java
class SumToN {
private int num, sum;
public SumToN(int aNum) throws IllegalArgumentException {
setNum(aNum);
}
public void setNum(int aNum) throws IllegalArgumentException {
if (aNum < 0) {
throw new IllegalArgumentException(aNum + " < 0");
}
num = aNum;
}
public int calcSum() {
sum = 0;
for (int i = 0; i <= num; i++) {sum += i;}
return sum;
}
public void display() {
System.out.println("Sum of " + num + " nos = " + sum);
}
}
public class SumToN_WithException {
public static void main(String[] args) {
int n = -30;
SumToN a = new SumToN(n);
a.calcSum();
a.display();
}
}
$javac SumToN_WithException.java
$java SumToN_WithException
Exception in thread "main" java.lang.IllegalArgumentException: -30 < 0
at SumToN.setNum(SumToN_WithException.java:9)
at SumToN.<init>(SumToN_WithException.java:5)
at SumToN_WithException.main(SumToN_WithException.java:28)
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
$vi SumToN_WithExceptionHandler.java
class SumToN {
private int num, sum;
public SumToN(int aNum) throws IllegalArgumentException {
setNum(aNum);
}
public void setNum(int aNum) throws IllegalArgumentException {
if (aNum < 0) {
throw new IllegalArgumentException(aNum + " < 0");
}
num = aNum;
}
public int calcSum() {
sum = 0;
for (int i = 0; i <= num; i++) {sum += i;}
return sum;
}
public void display() {
System.out.println("Sum of " + num + " nos = " + sum);
}
}
public class SumToN_WithExceptionHandler {
public static void main(String[] args) {
int n = -30;
//try/catch block is not a must, because
// IllegalArgumentException is an unchecked exception,
// Why then provided the custom handler ???
try {
SumToN a = new SumToN(n);
System.out.println("This statement is not executed");
a.calcSum();
a.display();
} catch ( Exception e ) {
System.out.println("my handler : " + e.getMessage());
}
System.out.println("This statement is executed");
}
}
$javac SumToN_WithExceptionHandler.java
$java SumToN_WithExceptionHandler
my handler : -30 < 0
This statement is executed