-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumeric Exceptions.java
63 lines (59 loc) · 1.11 KB
/
Numeric Exceptions.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
import java.util.*;
class NumericException extends Exception
{
public String toString()
{
return("Operand Can only be a numeric value");
}
}
public class Exp7prog3 {
static void checkoperand(String op) throws NumericException
{ char[] digits=op.toCharArray();
for(int i=0;i<op.length();i++)
{
if(!Character.isDigit(digits[i]))
throw new NumericException();
}
}
public static void main(String args[])
{
try
{
checkoperand(args[0]);
try
{
checkoperand(args[1]);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
catch(Exception e1)
{
System.out.println(e1.toString());
}
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
float result=0.0f;
char[] operator=args[2].toCharArray();
switch(operator[0])
{
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
default:
System.out.println("Invalid oprator !");
}
System.out.println("REsuult is : "+result);
}
}