-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathException_Class.java
More file actions
74 lines (64 loc) · 1.89 KB
/
Copy pathException_Class.java
File metadata and controls
74 lines (64 loc) · 1.89 KB
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
74
package BASICS;
import java.util.Scanner;
class MyException extends Exception{
@Override
public String toString() {
return super.toString() + " I am toString";
}
@Override
public String getMessage() {
return super.getMessage() + " Im getMessage";
}
}
class Negative extends Exception{
@Override
public String toString() {
return "Radius cannot be negetive";
}
@Override
public String getMessage() {
return "Radius cannot be negetive!";
}
}
public class Exception_Class {
public static double area (int r) throws Negative{
if(r<0){
throw new Negative();
}
double result = Math.PI*r*r;
return result;
}
public static int divide(int a, int b) throws ArithmeticException{
int result = a/b;
return result;
}
public static void main(String[] args) {
int a;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
if(a < 9){
try {
throw new MyException();
// throw new ArithmeticException("This is Arithmetic Exception");
}
catch(Exception e){
System.out.println(e.toString());
System.out.println(e.getMessage());
System.out.println(e); // here ToString will run by default
e.printStackTrace(); // will display in the end
System.out.println("Finished");
}
System.out.println("Yes FInished");
}
try{
// int c = divide(6,0);
// System.out.println(c);
double ar = area(-6); //now even if functions handles exception, we have to put it in try
System.out.println(ar);
}
catch(Exception e){
System.out.println("Try block 2 Evoked");
System.out.println(e);
}
}
}